Skip to content

Commit

Permalink
Fixed #16207 -- Enhanced documentation about user profile model insta…
Browse files Browse the repository at this point in the history
…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
ramiro committed Jun 25, 2011
1 parent a311769 commit 2d6dec2
Showing 1 changed file with 34 additions and 10 deletions.
44 changes: 34 additions & 10 deletions docs/topics/auth.txt
Original file line number Diff line number Diff line change
Expand Up @@ -262,9 +262,10 @@ Methods

Returns a site-specific profile for this user. Raises
:exc:`django.contrib.auth.models.SiteProfileNotAvailable` if the
current site doesn't allow profiles. For information on how to define a
site-specific user profile, see the section on `storing additional user
information`_ below.
current site doesn't allow profiles, or
:exc:`django.core.exceptions.ObjectDoesNotExist` if the user does not
have a profile. For information on how to define a site-specific user
profile, see the section on `storing additional user information`_ below.

.. _storing additional user information: #storing-additional-information-about-users

Expand Down Expand Up @@ -470,7 +471,18 @@ you'd like to have available, and also add a
:class:`~django.db.models.Field.OneToOneField` named ``user`` from your model
to the :class:`~django.contrib.auth.models.User` model. This will ensure only
one instance of your model can be created for each
:class:`~django.contrib.auth.models.User`.
:class:`~django.contrib.auth.models.User`. For example::

from django.contrib.auth.models import User

class UserProfile(models.Model):
# This field is required.
user = models.OneToOneField(User)

# Other fields here
accepted_eula = models.BooleanField()
favorite_animal = models.CharField(max_length=20, default="Dragons.")


To indicate that this model is the user profile model for a given site, fill in
the setting :setting:`AUTH_PROFILE_MODULE` with a string consisting of the
Expand All @@ -496,14 +508,26 @@ instance of the user profile model associated with that
:class:`~django.contrib.auth.models.User`.

The method :class:`~django.contrib.auth.models.User.get_profile()`
does not create the profile, if it does not exist. You need to
register a handler for the signal
:attr:`django.db.models.signals.post_save` on the User model, and, in
the handler, if created=True, create the associated user profile.
does not create a profile if one does not exist. You need to register a handler
for the User model's :attr:`django.db.models.signals.post_save` signal and, in
the handler, if ``created`` is ``True``, create the associated user profile::

# in models.py

from django.contrib.auth.models import User
from django.db.models.signals import post_save

# definition of UserProfile from above
# ...

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

For more information, see `Chapter 12 of the Django book`_.
post_save.connect(create_user_profile, sender=User)

.. _Chapter 12 of the Django book: http://www.djangobook.com/en/1.0/chapter12/#cn222
.. seealso:: :doc:`/topics/signals` for more information on Django's signal
dispatcher.

Authentication in Web requests
==============================
Expand Down

0 comments on commit 2d6dec2

Please sign in to comment.