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.
|
|
|
|
"""
|
|
|
|
|
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()
|
2006-02-04 04:03:04 +08:00
|
|
|
primary_categories = models.ManyToManyField(Category, related_name='primary_article_set')
|
|
|
|
secondary_categories = models.ManyToManyField(Category, related_name='secondary_article_set')
|
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()
|
2006-02-04 07:30:12 +08:00
|
|
|
>>> a1.primary_categories.add(c2, c3)
|
|
|
|
>>> a1.secondary_categories.add(c4)
|
2005-12-01 14:01:46 +08:00
|
|
|
|
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()
|
2006-02-04 07:30:12 +08:00
|
|
|
>>> a2.primary_categories.add(c1, c2)
|
|
|
|
>>> a2.secondary_categories.add(c4)
|
2005-12-01 14:01:46 +08:00
|
|
|
|
2006-02-04 04:03:04 +08:00
|
|
|
>>> a1.primary_categories.all()
|
2005-12-01 14:01:46 +08:00
|
|
|
[Crime, News]
|
|
|
|
|
2006-02-04 04:03:04 +08:00
|
|
|
>>> a2.primary_categories.all()
|
2005-12-01 14:01:46 +08:00
|
|
|
[News, Sports]
|
|
|
|
|
2006-02-04 04:03:04 +08:00
|
|
|
>>> a1.secondary_categories.all()
|
2005-12-01 14:01:46 +08:00
|
|
|
[Life]
|
|
|
|
|
|
|
|
|
2006-01-31 09:08:02 +08:00
|
|
|
>>> c1.primary_article_set.all()
|
2005-12-01 14:01:46 +08:00
|
|
|
[Area man runs]
|
2006-01-31 09:08:02 +08:00
|
|
|
>>> c1.secondary_article_set.all()
|
2005-12-01 14:01:46 +08:00
|
|
|
[]
|
2006-01-31 09:08:02 +08:00
|
|
|
>>> c2.primary_article_set.all()
|
2005-12-01 14:01:46 +08:00
|
|
|
[Area man steals, Area man runs]
|
2006-01-31 09:08:02 +08:00
|
|
|
>>> c2.secondary_article_set.all()
|
2005-12-01 14:01:46 +08:00
|
|
|
[]
|
2006-01-31 09:08:02 +08:00
|
|
|
>>> c3.primary_article_set.all()
|
2005-12-01 14:01:46 +08:00
|
|
|
[Area man steals]
|
2006-01-31 09:08:02 +08:00
|
|
|
>>> c3.secondary_article_set.all()
|
2005-12-01 14:01:46 +08:00
|
|
|
[]
|
2006-01-31 09:08:02 +08:00
|
|
|
>>> c4.primary_article_set.all()
|
2005-12-01 14:01:46 +08:00
|
|
|
[]
|
2006-01-31 09:08:02 +08:00
|
|
|
>>> c4.secondary_article_set.all()
|
2005-12-01 14:01:46 +08:00
|
|
|
[Area man steals, Area man runs]
|
|
|
|
"""
|