Check out this blog post for a one line solution to the same problem.

http://www.turnkeylinux.org/blog/django-profile

On Mar 16, 11:54 am, Ori Livneh <ori.liv...@gmail.com> wrote:
> Hi guys,
>
> The Django docs explain that "the method get_profile() does not create the
> profile, if it does not exist. You need to register a handler for the signal
> django.db.models.signals.post_save on the User model, and, in the handler,
> if created=True, create the associated user profile." (http://goo.gl/jNo91)
>
> But there's no code sample. A few weeks ago (with some help from friendly
> people on #django) I came up with this snippet:
>
> # ~ snippet start ~
>
> @receiver(post_save, sender=User)
> def create_profile(sender, instance, created, **kwargs):
>     """Create a matching profile whenever a User is created."""
>     if created:
>         profile, new = UserProfile.objects.get_or_create(user=instance)
>
> # ~ snippet end ~
>
> (I use get_or_create as extra insurance against cases wherein a User is
> created, deleted, and then created anew.)
>
> Is there anything in this snippet that should be fixed or improved?
>
> If it's OK, do you think it makes sense to include it in the docs? I ask
> because getting user profiles to work is liable to be something new Django
> developers want to do, but signals are something of an intermediate/advanced
> topic.
>
> Thanks,
> Ori
>
> PS I wrote this up on my blog with a slightly lengthier explanation, in case
> anyone finds it 
> useful:http://floru.it/2011/using-signals-to-create-user-profiles-in-django-...

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to