2005-12-01 14:01:46 +08:00
|
|
|
"""
|
|
|
|
20. Multiple many-to-many relationships between the same two tables
|
|
|
|
|
|
|
|
In this example, an Article can have many Categories (as "primary") and many
|
|
|
|
Categories (as "secondary").
|
|
|
|
|
|
|
|
Set ``related_name`` to designate what the reverse relationship is called.
|
|
|
|
|
|
|
|
Set ``singular`` to designate what the category object is called. This is
|
|
|
|
required if a model has multiple ``ManyToManyFields`` to the same object.
|
|
|
|
"""
|
|
|
|
|
2005-12-14 13:02:51 +08:00
|
|
|
from django.db import models
|
2005-12-01 14:01:46 +08:00
|
|
|
|
2005-12-14 13:02:51 +08:00
|
|
|
class Category(models.Model):
|
|
|
|
name = models.CharField(maxlength=20)
|
2005-12-21 10:42:27 +08:00
|
|
|
class Meta:
|
2005-12-01 14:01:46 +08:00
|
|
|
ordering = ('name',)
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return self.name
|
|
|
|
|
2005-12-14 13:02:51 +08:00
|
|
|
class Article(models.Model):
|
|
|
|
headline = models.CharField(maxlength=50)
|
|
|
|
pub_date = models.DateTimeField()
|
|
|
|
primary_categories = models.ManyToManyField(Category,
|
2005-12-01 14:01:46 +08:00
|
|
|
singular='primary_category', related_name='primary_article')
|
2005-12-14 13:02:51 +08:00
|
|
|
secondary_categories = models.ManyToManyField(Category,
|
2005-12-01 14:01:46 +08:00
|
|
|
singular='secondary_category', related_name='secondary_article')
|
2005-12-21 10:42:27 +08:00
|
|
|
class Meta:
|
2005-12-01 14:01:46 +08:00
|
|
|
ordering = ('pub_date',)
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return self.headline
|
|
|
|
|
|
|
|
API_TESTS = """
|
|
|
|
>>> from datetime import datetime
|
|
|
|
|
2005-12-12 06:10:02 +08:00
|
|
|
>>> c1 = Category(name='Sports')
|
2005-12-01 14:01:46 +08:00
|
|
|
>>> c1.save()
|
2005-12-12 06:10:02 +08:00
|
|
|
>>> c2 = Category(name='News')
|
2005-12-01 14:01:46 +08:00
|
|
|
>>> c2.save()
|
2005-12-12 06:10:02 +08:00
|
|
|
>>> c3 = Category(name='Crime')
|
2005-12-01 14:01:46 +08:00
|
|
|
>>> c3.save()
|
2005-12-12 06:10:02 +08:00
|
|
|
>>> c4 = Category(name='Life')
|
2005-12-01 14:01:46 +08:00
|
|
|
>>> c4.save()
|
|
|
|
|
2005-12-12 06:10:02 +08:00
|
|
|
>>> a1 = Article(headline='Area man steals', pub_date=datetime(2005, 11, 27))
|
2005-12-01 14:01:46 +08:00
|
|
|
>>> a1.save()
|
|
|
|
>>> a1.set_primary_categories([c2.id, c3.id])
|
|
|
|
True
|
|
|
|
>>> a1.set_secondary_categories([c4.id])
|
|
|
|
True
|
|
|
|
|
2005-12-12 06:10:02 +08:00
|
|
|
>>> a2 = Article(headline='Area man runs', pub_date=datetime(2005, 11, 28))
|
2005-12-01 14:01:46 +08:00
|
|
|
>>> a2.save()
|
|
|
|
>>> a2.set_primary_categories([c1.id, c2.id])
|
|
|
|
True
|
|
|
|
>>> a2.set_secondary_categories([c4.id])
|
|
|
|
True
|
|
|
|
|
|
|
|
# The "primary_category" here comes from the "singular" parameter. If we hadn't
|
|
|
|
# specified the "singular" parameter, Django would just use "category", which
|
|
|
|
# would cause a conflict because the "primary_categories" and
|
|
|
|
# "secondary_categories" fields both relate to Category.
|
|
|
|
>>> a1.get_primary_category_list()
|
|
|
|
[Crime, News]
|
|
|
|
|
|
|
|
# Ditto for the "primary_category" here.
|
|
|
|
>>> a2.get_primary_category_list()
|
|
|
|
[News, Sports]
|
|
|
|
|
|
|
|
# Ditto for the "secondary_category" here.
|
|
|
|
>>> a1.get_secondary_category_list()
|
|
|
|
[Life]
|
|
|
|
|
|
|
|
# Ditto for the "secondary_category" here.
|
|
|
|
>>> a2.get_secondary_category_list()
|
|
|
|
[Life]
|
|
|
|
|
|
|
|
|
|
|
|
>>> c1.get_primary_article_list()
|
|
|
|
[Area man runs]
|
|
|
|
>>> c1.get_secondary_article_list()
|
|
|
|
[]
|
|
|
|
>>> c2.get_primary_article_list()
|
|
|
|
[Area man steals, Area man runs]
|
|
|
|
>>> c2.get_secondary_article_list()
|
|
|
|
[]
|
|
|
|
>>> c3.get_primary_article_list()
|
|
|
|
[Area man steals]
|
|
|
|
>>> c3.get_secondary_article_list()
|
|
|
|
[]
|
|
|
|
>>> c4.get_primary_article_list()
|
|
|
|
[]
|
|
|
|
>>> c4.get_secondary_article_list()
|
|
|
|
[Area man steals, Area man runs]
|
|
|
|
"""
|