Ticket #16207: 16207.2.diff

File 16207.2.diff, 3.0 KB (added by melinath, 4 years ago)
  • docs/topics/auth.txt

    diff --git a/docs/topics/auth.txt b/docs/topics/auth.txt
    index 12d538f..3a37341 100644
    a b Methods 
    262262
    263263        Returns a site-specific profile for this user. Raises
    264264        :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.
    268269
    269270.. _storing additional user information: #storing-additional-information-about-users
    270271
    you'd like to have available, and also add a 
    470471:class:`~django.db.models.Field.OneToOneField` named ``user`` from your model
    471472to the :class:`~django.contrib.auth.models.User` model. This will ensure only
    472473one 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
    474486
    475487To indicate that this model is the user profile model for a given site, fill in
    476488the setting :setting:`AUTH_PROFILE_MODULE` with a string consisting of the
    instance of the user profile model associated with that 
    496508:class:`~django.contrib.auth.models.User`.
    497509
    498510The 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.
     511does not create a profile if one does not exist. You need to register a handler
     512for the User model's :attr:`django.db.models.signals.post_save` signal and, in
     513the 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)
    503526
    504 For more information, see `Chapter 12 of the Django book`_.
     527    post_save.connect(create_user_profile, sender=User)
    505528
    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.
    507531
    508532Authentication in Web requests
    509533==============================
Back to Top