Opened 13 years ago

Closed 13 years ago

#16207 closed Bug (fixed)

UserProfile documentation (not extensive enough)

Reported by: Cal Leeming Owned by: Stephen Burrows
Component: Documentation Version: 1.3
Severity: Normal Keywords: userprofile
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

https://docs.djangoproject.com/en/dev/topics/auth/#storing-additional-information-about-users#

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.

However, it does not show how to do this. For new users, this will be quite off putting, especially as most people skim the documentation, and don't bother with the 'fat'.

May I suggest the documentation is included to use the following snippet:
http://stackoverflow.com/questions/44109/extending-the-user-model-with-custom-fields-in-django

#in models.py
from django.contrib.auth.models import User
from django.db.models.signals import post_save

class UserProfile(models.Model):  
    user = models.OneToOneField(User)  
    #other fields here

    def __str__(self):  
          return "%s's profile" % self.user  

def create_user_profile(sender, instance, created, **kwargs):  
    if created:  
       profile, created = UserProfile.objects.get_or_create(user=instance)  

post_save.connect(create_user_profile, sender=User) 

#in settings.py
AUTH_PROFILE_MODULE = 'YOURAPP.UserProfile'

Attachments (2)

16207.diff (3.0 KB ) - added by Stephen Burrows 13 years ago.
16207.2.diff (3.0 KB ) - added by Stephen Burrows 13 years ago.

Download all attachments as: .zip

Change History (13)

comment:1 by Aymeric Augustin, 13 years ago

Triage Stage: UnreviewedAccepted

Yes, illustrating the text with code samples would help. The link to the Django Book may become redundant, then.

comment:2 by Stephen Burrows, 13 years ago

Owner: changed from nobody to Stephen Burrows
Status: newassigned

by Stephen Burrows, 13 years ago

Attachment: 16207.diff added

comment:3 by Stephen Burrows, 13 years ago

Has patch: set
Keywords: userprofile added

comment:4 by Cal Leeming, 13 years ago

Nice, thanks for the quick turn around on this guys!

in reply to:  1 comment:5 by Julien Phalip, 13 years ago

Replying to aaugustin:

Yes, illustrating the text with code samples would help. The link to the Django Book may become redundant, then.

Yep, the link to the Django book should be removed. See #16102. Both tickets could be fixed in one go.

comment:6 by Stephen Burrows, 13 years ago

I already removed the link. Forgot to mention that, sorry.

comment:7 by Julien Phalip, 13 years ago

Patch needs improvement: set

OK, I've closed #16102 as a dupe.

This patch is looking good, however a few improvements should be made in the code snippet about the post_save signal:

  • Do not say "perhaps in models.py", but instead provide specific examples of where this code could be placed.
  • Since a new user is created, it is unnecessary to use UserProfile.objects.get_or_create. UserProfile.objects.create can be used instead.
  • unicorns and favorite_animal are both required fields, so you need to either make them optional or provide values for them when creating the UserProfile instance.

comment:8 by Julien Phalip, 13 years ago

Sorry, favorite_animal has a default value and unicorns is a M2M, so no need to provide values to UserProfile.objects.create for those.

by Stephen Burrows, 13 years ago

Attachment: 16207.2.diff added

comment:9 by Stephen Burrows, 13 years ago

Patch needs improvement: unset

This patch should fix the problems julien pointed out.

comment:10 by Julien Phalip, 13 years ago

Triage Stage: AcceptedReady for checkin

Looks good, thanks!

comment:11 by Ramiro Morales, 13 years ago

Resolution: fixed
Status: assignedclosed

In [16450]:

Fixed #16207 -- Enhanced documentation about user profile model instance creation. Thanks foxwhisper for the report, melinath for the patch and Julien for reviewing it.

Note: See TracTickets for help on using tickets.
Back to Top