Opened 4 years ago

Closed 4 years ago

#16207 closed Bug (fixed)

UserProfile documentation (not extensive enough)

Reported by: foxwhisper 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 4 years ago.
16207.2.diff (3.0 KB) - added by melinath 4 years ago.

Download all attachments as: .zip

Change History (13)

comment:1 follow-up: Changed 4 years ago by aaugustin

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Accepted

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

comment:2 Changed 4 years ago by melinath

  • Owner changed from nobody to melinath
  • Status changed from new to assigned

Changed 4 years ago by melinath

comment:3 Changed 4 years ago by melinath

  • Has patch set
  • Keywords userprofile added

comment:4 Changed 4 years ago by foxwhisper

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

comment:5 in reply to: ↑ 1 Changed 4 years ago by julien

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

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

comment:7 Changed 4 years ago by julien

  • 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 4 years ago by julien

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

comment:9 Changed 4 years ago by melinath

  • Patch needs improvement unset

This patch should fix the problems julien pointed out.

comment:10 Changed 4 years ago by julien

  • Triage Stage changed from Accepted to Ready for checkin

Looks good, thanks!

comment:11 Changed 4 years ago by ramiro

  • Resolution set to fixed
  • Status changed from assigned to closed

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