I saw this one-liner and didn't think it was a very good solution. I'd be interested to hear others weigh in.
It's not very readable, and readability counts. What is that [0] doing there? You have to rack your brain a little to recall that get_or_create returns a tuple of (object, created). Second, it can create bottlenecks. Imagine you have a query which iterates through a list of users and gets some attributes from the associated profile object. As it executes, this query would create an associated profile object for each user object which doesn't already have one, which could get very expensive. Third, it just seems better to me, from an architectural perspective, to save things when you're saving, and not scramble to conjure something up when someone's asking for it. By allowing access to user.profile before it exists, this one-liner is essentially in the business of pretending something exists when it doesn't, and that seems unpythonic and dangerous to me. Again, though, I'd be interested to hear other people weigh in. OL On Wed, Mar 16, 2011 at 7:46 PM, shantp <sha...@gmail.com> wrote: > 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. > > -- 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.