/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. """
  2. 9. Many-to-many relationships via an intermediary table
  3. For many-to-many relationships that need extra fields on the intermediary
  4. table, use an intermediary model.
  5. In this example, an ``Article`` can have multiple ``Reporter`` objects, and
  6. each ``Article``-``Reporter`` combination (a ``Writer``) has a ``position``
  7. field, which specifies the ``Reporter``'s position for the given article
  8. (e.g. "Staff writer").
  9. """
  10. from django.db import models
  11. class Reporter(models.Model):
  12. first_name = models.CharField(max_length=30)
  13. last_name = models.CharField(max_length=30)
  14. def __unicode__(self):
  15. return u"%s %s" % (self.first_name, self.last_name)
  16. class Article(models.Model):
  17. headline = models.CharField(max_length=100)
  18. pub_date = models.DateField()
  19. def __unicode__(self):
  20. return self.headline
  21. class Writer(models.Model):
  22. reporter = models.ForeignKey(Reporter)
  23. article = models.ForeignKey(Article)
  24. position = models.CharField(max_length=100)
  25. def __unicode__(self):
  26. return u'%s (%s)' % (self.reporter, self.position)