/tests/modeltests/m2m_multiple/models.py

https://code.google.com/p/mango-py/ · Python · 30 lines · 21 code · 6 blank · 3 comment · 0 complexity · a95e247c8e36219cee0f25f8347db307 MD5 · raw file

  1. """
  2. 20. Multiple many-to-many relationships between the same two tables
  3. In this example, an ``Article`` can have many "primary" ``Category`` objects
  4. and many "secondary" ``Category`` objects.
  5. Set ``related_name`` to designate what the reverse relationship is called.
  6. """
  7. from django.db import models
  8. class Category(models.Model):
  9. name = models.CharField(max_length=20)
  10. class Meta:
  11. ordering = ('name',)
  12. def __unicode__(self):
  13. return self.name
  14. class Article(models.Model):
  15. headline = models.CharField(max_length=50)
  16. pub_date = models.DateTimeField()
  17. primary_categories = models.ManyToManyField(Category, related_name='primary_article_set')
  18. secondary_categories = models.ManyToManyField(Category, related_name='secondary_article_set')
  19. class Meta:
  20. ordering = ('pub_date',)
  21. def __unicode__(self):
  22. return self.headline