/tests/modeltests/m2m_multiple/models.py
Python | 30 lines | 27 code | 0 blank | 3 comment | 0 complexity | a95e247c8e36219cee0f25f8347db307 MD5 | raw file
1""" 220. Multiple many-to-many relationships between the same two tables 3 4In this example, an ``Article`` can have many "primary" ``Category`` objects 5and many "secondary" ``Category`` objects. 6 7Set ``related_name`` to designate what the reverse relationship is called. 8""" 9 10from django.db import models 11 12class Category(models.Model): 13 name = models.CharField(max_length=20) 14 class Meta: 15 ordering = ('name',) 16 17 def __unicode__(self): 18 return self.name 19 20class Article(models.Model): 21 headline = models.CharField(max_length=50) 22 pub_date = models.DateTimeField() 23 primary_categories = models.ManyToManyField(Category, related_name='primary_article_set') 24 secondary_categories = models.ManyToManyField(Category, related_name='secondary_article_set') 25 class Meta: 26 ordering = ('pub_date',) 27 28 def __unicode__(self): 29 return self.headline 30