diff options
| author | Julien Phalip <jphalip@gmail.com> | 2011-09-11 19:28:18 +0000 |
|---|---|---|
| committer | Julien Phalip <jphalip@gmail.com> | 2011-09-11 19:28:18 +0000 |
| commit | fd9045346286208c0dbd0afc1f820e868910dac8 (patch) | |
| tree | 501f6ace1f1c430292f5235e82a2564aba9750c2 /docs | |
| parent | 7f45651f8db35a9f5cd5a5822c133cefb607aa40 (diff) | |
Fixed #16742 -- Provided code examples in the models documentation for accessing extra fields on many-to-many relationships. Many thanks to aj for the suggestion and to Nick Lang for the patch.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@16817 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'docs')
| -rw-r--r-- | docs/topics/db/models.txt | 21 | ||||
| -rw-r--r-- | docs/topics/db/queries.txt | 2 |
2 files changed, 22 insertions, 1 deletions
diff --git a/docs/topics/db/models.txt b/docs/topics/db/models.txt index f4302e9746..465d9193a4 100644 --- a/docs/topics/db/models.txt +++ b/docs/topics/db/models.txt @@ -325,7 +325,7 @@ whatever you want. For example:: For details on accessing backwards-related objects, see the `Following relationships backward example`_. - + For sample code, see the `Many-to-one relationship model tests`_. .. _Following relationships backward example: http://docs.djangoproject.com/en/dev/topics/db/queries/#backwards-related-objects @@ -519,6 +519,25 @@ As you are using an intermediate model, you can also query on its attributes:: ... membership__date_joined__gt=date(1961,1,1)) [<Person: Ringo Starr] +If you need to access a membership's information you may do so by directly +querying the ``Membership`` model:: + + >>> ringos_membership = Membership.objects.get(group=beatles, person=ringo) + >>> ringos_membership.date_joined + datetime.date(1962, 8, 16) + >>> ringos_membership.invite_reason + u'Needed a new drummer.' + +Another way to access the same information is by querying the +:ref:`many-to-many reverse relationship<m2m-reverse-relationships>` from a +``Person`` object:: + + >>> ringos_membership = ringo.membership_set.get(group=beatles) + >>> ringos_membership.date_joined + datetime.date(1962, 8, 16) + >>> ringos_membership.invite_reason + u'Needed a new drummer.' + One-to-one relationships ~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/docs/topics/db/queries.txt b/docs/topics/db/queries.txt index 8e2c0cc021..23ed124fa5 100644 --- a/docs/topics/db/queries.txt +++ b/docs/topics/db/queries.txt @@ -1036,6 +1036,8 @@ Each "reverse" operation described in this section has an immediate effect on the database. Every addition, creation and deletion is immediately and automatically saved to the database. +.. _m2m-reverse-relationships: + Many-to-many relationships -------------------------- |
