Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #16207 -- Enhanced documentation about user profile model insta…

…nce creation. Thanks foxwhisper for the report, melinath for the patch and Julien for reviewing it.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@16450 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 2d6dec24c43423f71895ab0fd1b41a0b5b6c2e8a 1 parent a311769
Ramiro Morales authored June 25, 2011

Showing 1 changed file with 34 additions and 10 deletions. Show diff stats Hide diff stats

  1. 44  docs/topics/auth.txt
44  docs/topics/auth.txt
@@ -262,9 +262,10 @@ Methods
262 262
 
263 263
         Returns a site-specific profile for this user. Raises
264 264
         :exc:`django.contrib.auth.models.SiteProfileNotAvailable` if the
265  
-        current site doesn't allow profiles. For information on how to define a
266  
-        site-specific user profile, see the section on `storing additional user
267  
-        information`_ below.
  265
+        current site doesn't allow profiles, or
  266
+        :exc:`django.core.exceptions.ObjectDoesNotExist` if the user does not
  267
+        have a profile. For information on how to define a site-specific user
  268
+        profile, see the section on `storing additional user information`_ below.
268 269
 
269 270
 .. _storing additional user information: #storing-additional-information-about-users
270 271
 
@@ -470,7 +471,18 @@ you'd like to have available, and also add a
470 471
 :class:`~django.db.models.Field.OneToOneField` named ``user`` from your model
471 472
 to the :class:`~django.contrib.auth.models.User` model. This will ensure only
472 473
 one instance of your model can be created for each
473  
-:class:`~django.contrib.auth.models.User`.
  474
+:class:`~django.contrib.auth.models.User`. For example::
  475
+
  476
+    from django.contrib.auth.models import User
  477
+
  478
+    class UserProfile(models.Model):
  479
+        # This field is required.
  480
+        user = models.OneToOneField(User)
  481
+
  482
+        # Other fields here
  483
+        accepted_eula = models.BooleanField()
  484
+        favorite_animal = models.CharField(max_length=20, default="Dragons.")
  485
+
474 486
 
475 487
 To indicate that this model is the user profile model for a given site, fill in
476 488
 the setting :setting:`AUTH_PROFILE_MODULE` with a string consisting of the
@@ -496,14 +508,26 @@ instance of the user profile model associated with that
496 508
 :class:`~django.contrib.auth.models.User`.
497 509
 
498 510
 The method :class:`~django.contrib.auth.models.User.get_profile()`
499  
-does not create the profile, if it does not exist. You need to
500  
-register a handler for the signal
501  
-:attr:`django.db.models.signals.post_save` on the User model, and, in
502  
-the handler, if created=True, create the associated user profile.
  511
+does not create a profile if one does not exist. You need to register a handler
  512
+for the User model's :attr:`django.db.models.signals.post_save` signal and, in
  513
+the handler, if ``created`` is ``True``, create the associated user profile::
  514
+
  515
+    # in models.py
  516
+
  517
+    from django.contrib.auth.models import User
  518
+    from django.db.models.signals import post_save
  519
+
  520
+    # definition of UserProfile from above
  521
+    # ...
  522
+
  523
+    def create_user_profile(sender, instance, created, **kwargs):
  524
+        if created:
  525
+            UserProfile.objects.create(user=instance)
503 526
 
504  
-For more information, see `Chapter 12 of the Django book`_.
  527
+    post_save.connect(create_user_profile, sender=User)
505 528
 
506  
-.. _Chapter 12 of the Django book: http://www.djangobook.com/en/1.0/chapter12/#cn222
  529
+.. seealso:: :doc:`/topics/signals` for more information on Django's signal
  530
+    dispatcher.
507 531
 
508 532
 Authentication in Web requests
509 533
 ==============================

0 notes on commit 2d6dec2

Please sign in to comment.
Something went wrong with that request. Please try again.