On Sun, 2009-03-08 at 23:30 -0700, myst3rious wrote:
> Hi,
> Working on a Django app, which requires profile for each subscribing
> user, I created a Profile model:
> class Profile(models.model):
> user = models.ForeignKey(User, unique=True)
> middlename = models.CharField(max_length=32, blank=True)
> gender = models.CharField(max_length=1, blank=True, null=True,
> choices=GENDER_LIST)
> last_updated = models.DateTimeField(auto_now=True)
>
> Then, there is another apps installed, which handles the registration.
> On successful activation of account, I need to create the entry in
> django.contrib.auth.models.User as well as this Profile. Right now,
> what I am doing is :
>
> def create_account(username, password, firstname, lastname, email):
> new_user = User.objects.create_user(
> username=username,
> email=email,
> password=password
> ) # This will create entry in
>
> new_user.is_active = True
> new_user.first_name = firstname
> new_user.last_name = lastname
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()
>
> # 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)
> return True
>
>
> # ----
>
> But I feel somehow, there would be better way to do this. Like, I can
> use signals (though not much familiar with this) or other way. Can
> anyone suggest me some better way, methods, algo, so the code be
> elegant, more understandable, better flow and more pythonic.
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
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---