2006-05-02 09:31:56 +08:00
|
|
|
"""
|
|
|
|
12. Relating a model to another model more than once
|
|
|
|
|
|
|
|
In this example, a ``Person`` can have a ``mother`` and ``father`` -- both of
|
|
|
|
which are other ``Person`` objects.
|
|
|
|
|
|
|
|
Set ``related_name`` to designate what the reverse relationship is called.
|
|
|
|
"""
|
|
|
|
|
|
|
|
from django.db import models
|
|
|
|
|
|
|
|
class Person(models.Model):
|
|
|
|
full_name = models.CharField(maxlength=20)
|
|
|
|
mother = models.ForeignKey('self', null=True, related_name='mothers_child_set')
|
|
|
|
father = models.ForeignKey('self', null=True, related_name='fathers_child_set')
|
|
|
|
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
def __unicode__(self):
|
2006-05-02 09:31:56 +08:00
|
|
|
return self.full_name
|
|
|
|
|
2006-08-27 21:59:47 +08:00
|
|
|
__test__ = {'API_TESTS':"""
|
2006-05-02 09:31:56 +08:00
|
|
|
# Create two Person objects -- the mom and dad in our family.
|
|
|
|
>>> dad = Person(full_name='John Smith Senior', mother=None, father=None)
|
|
|
|
>>> dad.save()
|
|
|
|
>>> mom = Person(full_name='Jane Smith', mother=None, father=None)
|
|
|
|
>>> mom.save()
|
|
|
|
|
|
|
|
# Give mom and dad a kid.
|
|
|
|
>>> kid = Person(full_name='John Smith Junior', mother=mom, father=dad)
|
|
|
|
>>> kid.save()
|
|
|
|
|
|
|
|
>>> kid.mother
|
2006-06-04 08:23:51 +08:00
|
|
|
<Person: Jane Smith>
|
2006-05-02 09:31:56 +08:00
|
|
|
>>> kid.father
|
2006-06-04 08:23:51 +08:00
|
|
|
<Person: John Smith Senior>
|
2006-05-02 09:31:56 +08:00
|
|
|
>>> dad.fathers_child_set.all()
|
2006-06-04 08:23:51 +08:00
|
|
|
[<Person: John Smith Junior>]
|
2006-05-02 09:31:56 +08:00
|
|
|
>>> mom.mothers_child_set.all()
|
2006-06-04 08:23:51 +08:00
|
|
|
[<Person: John Smith Junior>]
|
2006-05-02 09:31:56 +08:00
|
|
|
>>> kid.mothers_child_set.all()
|
|
|
|
[]
|
|
|
|
>>> kid.fathers_child_set.all()
|
|
|
|
[]
|
2006-08-27 21:59:47 +08:00
|
|
|
"""}
|