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