Ticket #16742: access_extra_fields_on_through_model.diff

File access_extra_fields_on_through_model.diff, 1.1 KB (added by Nick Lang, 13 years ago)

patch: example of accessing fields of a through model for a many to many

  • docs/topics/db/models.txt

     
    519519    ...     membership__date_joined__gt=date(1961,1,1))
    520520    [<Person: Ringo Starr]
    521521
     522If you were wanted to access any of the membership information from a Person
     523you would first have to query the Membership model and then you can access
     524the fields related to the membership::
    522525
     526    >>> ringos_membership = Membership.objects.get(group=beatles, person=ringo)
     527    >>> ringos_membership.date_joined
     528    datetime.date(1962, 8, 16)
     529    >>> ringos_membership.invite_reason
     530    u'Needed a new drummer.'
     531
     532Another way to access the same fields would be to use the reverse lookup from
     533``ringo``::
     534
     535    >>> ringos_membership = ringo.membership_set.get(group=beatles)
     536    >>> ringos_membership.date_joined
     537    datetime.date(1962, 8, 16)
     538    >>> ringos_membership.invite_reason
     539    u'Needed a new drummer.'
     540
     541
    523542One-to-one relationships
    524543~~~~~~~~~~~~~~~~~~~~~~~~
    525544
Back to Top