2006-05-02 03:31:56 +02:00
|
|
|
"""
|
2014-09-24 07:13:13 +02:00
|
|
|
Multiple many-to-many relationships between the same two tables
|
2006-05-02 03:31:56 +02:00
|
|
|
|
2008-08-12 16:15:38 +02:00
|
|
|
In this example, an ``Article`` can have many "primary" ``Category`` objects
|
|
|
|
and many "secondary" ``Category`` objects.
|
2006-05-02 03:31:56 +02:00
|
|
|
|
|
|
|
Set ``related_name`` to designate what the reverse relationship is called.
|
|
|
|
"""
|
|
|
|
|
|
|
|
from django.db import models
|
|
|
|
|
2011-10-13 20:04:12 +02:00
|
|
|
|
2006-05-02 03:31:56 +02:00
|
|
|
class Category(models.Model):
|
2007-08-05 07:14:46 +02:00
|
|
|
name = models.CharField(max_length=20)
|
2013-09-03 20:22:21 +02:00
|
|
|
|
2006-05-02 03:31:56 +02:00
|
|
|
class Meta:
|
2013-09-03 20:22:21 +02:00
|
|
|
ordering = ("name",)
|
2006-05-02 03:31:56 +02:00
|
|
|
|
2012-08-12 12:32:08 +02:00
|
|
|
def __str__(self):
|
2006-05-02 03:31:56 +02:00
|
|
|
return self.name
|
|
|
|
|
2013-09-03 20:22:21 +02:00
|
|
|
|
2006-05-02 03:31:56 +02:00
|
|
|
class Article(models.Model):
|
2007-08-05 07:14:46 +02:00
|
|
|
headline = models.CharField(max_length=50)
|
2006-05-02 03:31:56 +02:00
|
|
|
pub_date = models.DateTimeField()
|
|
|
|
primary_categories = models.ManyToManyField(
|
|
|
|
Category, related_name="primary_article_set"
|
|
|
|
)
|
|
|
|
secondary_categories = models.ManyToManyField(
|
|
|
|
Category, related_name="secondary_article_set"
|
|
|
|
)
|
2013-09-03 20:22:21 +02:00
|
|
|
|
2006-05-02 03:31:56 +02:00
|
|
|
class Meta:
|
2013-09-03 20:22:21 +02:00
|
|
|
ordering = ("pub_date",)
|
2006-05-02 03:31:56 +02:00
|
|
|
|
2012-08-12 12:32:08 +02:00
|
|
|
def __str__(self):
|
2006-05-02 03:31:56 +02:00
|
|
|
return self.headline
|