2006-05-02 09:31:56 +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.
|
|
|
|
"""
|
|
|
|
|
|
|
|
from django.db import models
|
|
|
|
|
|
|
|
class Category(models.Model):
|
|
|
|
name = models.CharField(maxlength=20)
|
|
|
|
class Meta:
|
|
|
|
ordering = ('name',)
|
|
|
|
|
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.name
|
|
|
|
|
|
|
|
class Article(models.Model):
|
|
|
|
headline = models.CharField(maxlength=50)
|
|
|
|
pub_date = models.DateTimeField()
|
|
|
|
primary_categories = models.ManyToManyField(Category, related_name='primary_article_set')
|
|
|
|
secondary_categories = models.ManyToManyField(Category, related_name='secondary_article_set')
|
|
|
|
class Meta:
|
|
|
|
ordering = ('pub_date',)
|
|
|
|
|
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.headline
|
|
|
|
|
2006-08-27 21:59:47 +08:00
|
|
|
__test__ = {'API_TESTS':"""
|
2006-05-02 09:31:56 +08:00
|
|
|
>>> from datetime import datetime
|
|
|
|
|
|
|
|
>>> c1 = Category(name='Sports')
|
|
|
|
>>> c1.save()
|
|
|
|
>>> c2 = Category(name='News')
|
|
|
|
>>> c2.save()
|
|
|
|
>>> c3 = Category(name='Crime')
|
|
|
|
>>> c3.save()
|
|
|
|
>>> c4 = Category(name='Life')
|
|
|
|
>>> c4.save()
|
|
|
|
|
|
|
|
>>> a1 = Article(headline='Area man steals', pub_date=datetime(2005, 11, 27))
|
|
|
|
>>> a1.save()
|
|
|
|
>>> a1.primary_categories.add(c2, c3)
|
|
|
|
>>> a1.secondary_categories.add(c4)
|
|
|
|
|
|
|
|
>>> a2 = Article(headline='Area man runs', pub_date=datetime(2005, 11, 28))
|
|
|
|
>>> a2.save()
|
|
|
|
>>> a2.primary_categories.add(c1, c2)
|
|
|
|
>>> a2.secondary_categories.add(c4)
|
|
|
|
|
|
|
|
>>> a1.primary_categories.all()
|
2006-06-04 08:23:51 +08:00
|
|
|
[<Category: Crime>, <Category: News>]
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
>>> a2.primary_categories.all()
|
2006-06-04 08:23:51 +08:00
|
|
|
[<Category: News>, <Category: Sports>]
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
>>> a1.secondary_categories.all()
|
2006-06-04 08:23:51 +08:00
|
|
|
[<Category: Life>]
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
|
|
|
|
>>> c1.primary_article_set.all()
|
2006-06-04 08:23:51 +08:00
|
|
|
[<Article: Area man runs>]
|
2006-05-02 09:31:56 +08:00
|
|
|
>>> c1.secondary_article_set.all()
|
|
|
|
[]
|
|
|
|
>>> c2.primary_article_set.all()
|
2006-06-04 08:23:51 +08:00
|
|
|
[<Article: Area man steals>, <Article: Area man runs>]
|
2006-05-02 09:31:56 +08:00
|
|
|
>>> c2.secondary_article_set.all()
|
|
|
|
[]
|
|
|
|
>>> c3.primary_article_set.all()
|
2006-06-04 08:23:51 +08:00
|
|
|
[<Article: Area man steals>]
|
2006-05-02 09:31:56 +08:00
|
|
|
>>> c3.secondary_article_set.all()
|
|
|
|
[]
|
|
|
|
>>> c4.primary_article_set.all()
|
|
|
|
[]
|
|
|
|
>>> c4.secondary_article_set.all()
|
2006-06-04 08:23:51 +08:00
|
|
|
[<Article: Area man steals>, <Article: Area man runs>]
|
2006-08-27 21:59:47 +08:00
|
|
|
"""}
|