PageRenderTime 101ms CodeModel.GetById 60ms app.highlight 3ms RepoModel.GetById 37ms app.codeStats 0ms

/tests/modeltests/m2m_intermediary/models.py

https://code.google.com/p/mango-py/
Python | 36 lines | 19 code | 0 blank | 17 comment | 1 complexity | 04acbaad11bc285e71659151b7d6f607 MD5 | raw file
 1"""
 29. Many-to-many relationships via an intermediary table
 3
 4For many-to-many relationships that need extra fields on the intermediary
 5table, use an intermediary model.
 6
 7In this example, an ``Article`` can have multiple ``Reporter`` objects, and
 8each ``Article``-``Reporter`` combination (a ``Writer``) has a ``position``
 9field, which specifies the ``Reporter``'s position for the given article
10(e.g. "Staff writer").
11"""
12
13from django.db import models
14
15class Reporter(models.Model):
16    first_name = models.CharField(max_length=30)
17    last_name = models.CharField(max_length=30)
18
19    def __unicode__(self):
20        return u"%s %s" % (self.first_name, self.last_name)
21
22class Article(models.Model):
23    headline = models.CharField(max_length=100)
24    pub_date = models.DateField()
25
26    def __unicode__(self):
27        return self.headline
28
29class Writer(models.Model):
30    reporter = models.ForeignKey(Reporter)
31    article = models.ForeignKey(Article)
32    position = models.CharField(max_length=100)
33
34    def __unicode__(self):
35        return u'%s (%s)' % (self.reporter, self.position)
36