2006-05-02 09:31:56 +08:00
|
|
|
"""
|
|
|
|
9. Many-to-many relationships via an intermediary table
|
|
|
|
|
|
|
|
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 ``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.db import models
|
|
|
|
|
|
|
|
class Reporter(models.Model):
|
2007-08-05 13:14:46 +08:00
|
|
|
first_name = models.CharField(max_length=30)
|
|
|
|
last_name = models.CharField(max_length=30)
|
2006-05-02 09:31:56 +08:00
|
|
|
|
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):
|
|
|
|
return u"%s %s" % (self.first_name, self.last_name)
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
class Article(models.Model):
|
2007-08-05 13:14:46 +08:00
|
|
|
headline = models.CharField(max_length=100)
|
2006-05-02 09:31:56 +08:00
|
|
|
pub_date = models.DateField()
|
|
|
|
|
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
|
|
|
|
|
|
|
|
class Writer(models.Model):
|
|
|
|
reporter = models.ForeignKey(Reporter)
|
|
|
|
article = models.ForeignKey(Article)
|
2007-08-05 13:14:46 +08:00
|
|
|
position = models.CharField(max_length=100)
|
2006-05-02 09:31:56 +08:00
|
|
|
|
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):
|
|
|
|
return u'%s (%s)' % (self.reporter, self.position)
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2006-08-27 21:59:47 +08:00
|
|
|
__test__ = {'API_TESTS':"""
|
2006-05-02 09:31:56 +08:00
|
|
|
# Create a few Reporters.
|
|
|
|
>>> r1 = Reporter(first_name='John', last_name='Smith')
|
|
|
|
>>> r1.save()
|
|
|
|
>>> r2 = Reporter(first_name='Jane', last_name='Doe')
|
|
|
|
>>> r2.save()
|
|
|
|
|
|
|
|
# Create an Article.
|
|
|
|
>>> from datetime import datetime
|
|
|
|
>>> a = Article(headline='This is a test', pub_date=datetime(2005, 7, 27))
|
|
|
|
>>> a.save()
|
|
|
|
|
|
|
|
# Create a few Writers.
|
|
|
|
>>> w1 = Writer(reporter=r1, article=a, position='Main writer')
|
|
|
|
>>> w1.save()
|
|
|
|
>>> w2 = Writer(reporter=r2, article=a, position='Contributor')
|
|
|
|
>>> w2.save()
|
|
|
|
|
|
|
|
# Play around with the API.
|
|
|
|
>>> a.writer_set.select_related().order_by('-position')
|
2006-06-04 08:23:51 +08:00
|
|
|
[<Writer: John Smith (Main writer)>, <Writer: Jane Doe (Contributor)>]
|
2006-05-02 09:31:56 +08:00
|
|
|
>>> w1.reporter
|
2006-06-04 08:23:51 +08:00
|
|
|
<Reporter: John Smith>
|
2006-05-02 09:31:56 +08:00
|
|
|
>>> w2.reporter
|
2006-06-04 08:23:51 +08:00
|
|
|
<Reporter: Jane Doe>
|
2006-05-02 09:31:56 +08:00
|
|
|
>>> w1.article
|
2006-06-04 08:23:51 +08:00
|
|
|
<Article: This is a test>
|
2006-05-02 09:31:56 +08:00
|
|
|
>>> w2.article
|
2006-06-04 08:23:51 +08:00
|
|
|
<Article: This is a test>
|
2006-05-02 09:31:56 +08:00
|
|
|
>>> r1.writer_set.all()
|
2006-06-04 08:23:51 +08:00
|
|
|
[<Writer: John Smith (Main writer)>]
|
2006-08-27 21:59:47 +08:00
|
|
|
"""}
|