Opened 5 years ago

Closed 5 years ago

#16207 closed Bug (fixed)

UserProfile documentation (not extensive enough)

Reported by: Cal Leeming Owned by: melinath
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 melinath 5 years ago.
16207.2.diff (3.0 KB) - added by melinath 5 years ago.

Download all attachments as: .zip

Change History (13)

comment:1 Changed 5 years ago by Aymeric Augustin

Triage Stage: UnreviewedAccepted

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

comment:2 Changed 5 years ago by melinath

Owner: changed from nobody to melinath
Status: newassigned

Changed 5 years ago by melinath

Attachment: 16207.diff added

comment:3 Changed 5 years ago by melinath

Has patch: set
Keywords: userprofile added

comment:4 Changed 5 years ago by Cal Leeming

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

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

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 Changed 5 years ago by melinath

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

comment:7 Changed 5 years ago by Julien Phalip

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 Changed 5 years ago by Julien Phalip

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

Changed 5 years ago by melinath

Attachment: 16207.2.diff added

comment:9 Changed 5 years ago by melinath

Patch needs improvement: unset

This patch should fix the problems julien pointed out.

comment:10 Changed 5 years ago by Julien Phalip

Triage Stage: AcceptedReady for checkin

Looks good, thanks!

comment:11 Changed 5 years ago by Ramiro Morales

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