Opened 14 years ago
Closed 14 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)
Change History (13)
follow-up: 5 comment:1 by , 14 years ago
| Triage Stage: | Unreviewed → Accepted | 
|---|
comment:2 by , 14 years ago
| Owner: | changed from to | 
|---|---|
| Status: | new → assigned | 
by , 14 years ago
| Attachment: | 16207.diff added | 
|---|
comment:3 by , 14 years ago
| Has patch: | set | 
|---|---|
| Keywords: | userprofile added | 
comment:5 by , 14 years ago
comment:7 by , 14 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.createcan be used instead.
- unicornsand- favorite_animalare both required fields, so you need to either make them optional or provide values for them when creating the- UserProfileinstance.
comment:8 by , 14 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 , 14 years ago
| Attachment: | 16207.2.diff added | 
|---|
comment:9 by , 14 years ago
| Patch needs improvement: | unset | 
|---|
This patch should fix the problems julien pointed out.
Yes, illustrating the text with code samples would help. The link to the Django Book may become redundant, then.