~ubuntu-branches/ubuntu/quantal/python-django/quantal-security

« back to all changes in this revision

Viewing changes to tests/modeltests/m2o_recursive2/models.py

  • Committer: Bazaar Package Importer
  • Author(s): Chris Lamb
  • Date: 2009-07-29 11:26:28 UTC
  • mfrom: (1.2.3 upstream)
  • mto: This revision was merged to the branch mainline in revision 22.
  • Revision ID: james.westby@ubuntu.com-20090729112628-9qrzwnl9x32jxhbg
Tags: upstream-1.1
ImportĀ upstreamĀ versionĀ 1.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
"""
 
2
12. Relating a model to another model more than once
 
3
 
 
4
In this example, a ``Person`` can have a ``mother`` and ``father`` -- both of
 
5
which are other ``Person`` objects.
 
6
 
 
7
Set ``related_name`` to designate what the reverse relationship is called.
 
8
"""
 
9
 
 
10
from django.db import models
 
11
 
 
12
class Person(models.Model):
 
13
    full_name = models.CharField(max_length=20)
 
14
    mother = models.ForeignKey('self', null=True, related_name='mothers_child_set')
 
15
    father = models.ForeignKey('self', null=True, related_name='fathers_child_set')
 
16
 
 
17
    def __unicode__(self):
 
18
        return self.full_name
 
19
 
 
20
__test__ = {'API_TESTS':"""
 
21
# Create two Person objects -- the mom and dad in our family.
 
22
>>> dad = Person(full_name='John Smith Senior', mother=None, father=None)
 
23
>>> dad.save()
 
24
>>> mom = Person(full_name='Jane Smith', mother=None, father=None)
 
25
>>> mom.save()
 
26
 
 
27
# Give mom and dad a kid.
 
28
>>> kid = Person(full_name='John Smith Junior', mother=mom, father=dad)
 
29
>>> kid.save()
 
30
 
 
31
>>> kid.mother
 
32
<Person: Jane Smith>
 
33
>>> kid.father
 
34
<Person: John Smith Senior>
 
35
>>> dad.fathers_child_set.all()
 
36
[<Person: John Smith Junior>]
 
37
>>> mom.mothers_child_set.all()
 
38
[<Person: John Smith Junior>]
 
39
>>> kid.mothers_child_set.all()
 
40
[]
 
41
>>> kid.fathers_child_set.all()
 
42
[]
 
43
"""}