Added m2o_recursive and m2o_recursive2 model unit tests/examples

git-svn-id: http://code.djangoproject.com/svn/django/trunk@356 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Adrian Holovaty 2005-07-31 01:23:47 +00:00
parent 04544c97b9
commit 2f04115694
4 changed files with 106 additions and 4 deletions

View File

@ -1 +1,3 @@
__all__ = ['basic', 'repr', 'custom_methods', 'many_to_one', 'many_to_many', 'ordering', 'lookup', 'get_latest', 'm2m_intermediary', 'one_to_one']
__all__ = ['basic', 'repr', 'custom_methods', 'many_to_one', 'many_to_many',
'ordering', 'lookup', 'get_latest', 'm2m_intermediary', 'one_to_one',
'm2o_recursive', 'm2o_recursive2']

View File

@ -4,9 +4,10 @@
For many-to-many relationships that need extra fields on the intermediary
table, use an intermediary model.
In this example, an article can have multiple reporters, and each
article-reporter combination (a "Writer") has a "position" field, which
specifies the reporter's position for the given article (e.g. "Staff writer").
In this example, an ``Article`` can have multiple ``Reporter``s, and each
``Article``-``Reporter`` combination (a ``Writer``) has a ``position`` field,
which specifies the ``Reporter``'s position for the given article (e.g. "Staff
writer").
"""
from django.core import meta

View File

@ -0,0 +1,48 @@
"""
11. Relating an object to itself, many-to-one
To define a many-to-one relationship between a model and itself, use
``ForeignKey('self')``.
In this example, a ``Category`` is related to itself. That is, each
``Category`` has a parent ``Category``.
Because of this recursive relationship, we need to tell Django what the
relationships should be called. Set ``rel_name`` for this, and set
``related_name`` to designate what the reverse relationship is called.
"""
from django.core import meta
class Category(meta.Model):
module_name = 'categories'
fields = (
meta.CharField('name', maxlength=20),
meta.ForeignKey('self', null=True,
rel_name='parent', related_name='child'),
)
def __repr__(self):
return self.name
API_TESTS = """
# Create a few Category objects.
>>> r = categories.Category(id=None, name='Root category', parent_id=None)
>>> r.save()
>>> c = categories.Category(id=None, name='Child category', parent_id=r.id)
>>> c.save()
>>> r.get_child_list()
[Child category]
>>> r.get_child(name__startswith='Child')
Child category
>>> r.get_parent()
Traceback (most recent call last):
...
CategoryDoesNotExist
>>> c.get_child_list()
[]
>>> c.get_parent()
Root category
"""

View File

@ -0,0 +1,51 @@
"""
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.
Because a ``Person`` has multiple relationships to ``Person``, we need to
distinguish the relationships. Set ``rel_name`` to tell Django what the
relationship should be called, because ``Person`` has two relationships to the
same model. Also, set ``related_name`` to designate what the reverse
relationship is called.
"""
from django.core import meta
class Person(meta.Model):
fields = (
meta.CharField('full_name', maxlength=20),
meta.ForeignKey('self', null=True, rel_name='mother',
related_name='mothers_child'),
meta.ForeignKey('self', null=True, rel_name='father',
related_name='fathers_child'),
)
def __repr__(self):
return self.full_name
API_TESTS = """
# Create two Person objects -- the mom and dad in our family.
>>> dad = persons.Person(id=None, full_name='John Smith Senior', mother_id=None, father_id=None)
>>> dad.save()
>>> mom = persons.Person(id=None, full_name='Jane Smith', mother_id=None, father_id=None)
>>> mom.save()
# Give mom and dad a kid.
>>> kid = persons.Person(id=None, full_name='John Smith Junior', mother_id=mom.id, father_id=dad.id)
>>> kid.save()
>>> kid.get_mother()
Jane Smith
>>> kid.get_father()
John Smith Senior
>>> dad.get_fathers_child_list()
[John Smith Junior]
>>> mom.get_mothers_child_list()
[John Smith Junior]
>>> kid.get_mothers_child_list()
[]
>>> kid.get_fathers_child_list()
[]
"""