diff --git a/docs/topics/auth.txt b/docs/topics/auth.txt
index 12d538f..3a37341 100644
a
|
b
|
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 | |
… |
… |
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 | unicorns = models.ManyToManyField(Unicorn) |
| 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 |
… |
… |
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 | ============================== |