On Mar 9, 1:06 am, myst3rious <[email protected]> wrote:
> > If you're setting all these things in the User object, then reverse the
> > logic slightly:
>
> > new_user = User.objects.create(username=username,
> > email=email,
> > is_active=True,
> > first_name=firstname,
> > last_name=lastname)
> > new_user.set_password(password)
> > new_user.save() # needed because set_password() doesn't call
> > save()
>
> > > new_user.save()
>
> ok, this is almost same as my code, well, I supplied the password in
> create() method. I can use the set_password, though.
>
>
>
> > > # Create Extended profile
> > > up=UserProfile.objects.create(user=new_user)
> > > up.firstname = new_user.first_name
> > > up.lastname = new_user.last_name
> > > up.save()
>
> > Again, this could become a one-liner (split over multiple lines for
> > presentation):
>
> > UserProfile.objects.create(user=new_user,
> > firstname=firstname,
> > lastname=lastname)
>
> > I'm not sure what you're seeing the problems are with your current
> > approach. You need to supply a minimum amount of information to the User
> > and UserProfile object to create them, so that says that certain
> > parameters are required. You're supplying those. I've shaved a couple of
> > lines off, but it's only minor stuff.
>
> > Given that minimal information requirement and the fact that you're
> > creating both objects in the same place -- a good thing so that nothing
> > is forgotten -- what else are you hoping to trim?
>
> > Regards,
> > Malcolm
>
> I am not hoping for the trims, and lesser code, just examples and
> suggestions from other users app, who dealt with the same profile
> thing and implementated some cool way to do it.
I also have a site that assumes each user has a profile (otherwise
bad).
I use django-registration, which sends out a couple signals. I hook
into one of those signals to create a user's profile when they
activate their account, like this:
# signal_receivers.py
from registration.signals import user_activated
from .models import UserProfile
def create_profile(sender, **kwargs):
UserProfile(user=kwargs['user']).save()
user_activated.connect(create_profile)
You could perhaps hook into user_registered instead, if errors pop up
between the time that a user registers and activates, but I haven't
had that problem since users are created as inactive by django-
registration until the user activates her account. This keeps your
database clean of UserProfile objects for users who never activate
their accounts.
Cheers,
Ross
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---