2006-05-02 09:31:56 +08:00
|
|
|
"""
|
2014-09-24 13:13:13 +08:00
|
|
|
Many-to-many relationships
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2008-08-12 22:15:38 +08:00
|
|
|
To define a many-to-many relationship, use ``ManyToManyField()``.
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2008-08-12 22:15:38 +08:00
|
|
|
In this example, an ``Article`` can be published in multiple ``Publication``
|
|
|
|
objects, and a ``Publication`` has multiple ``Article`` objects.
|
2006-05-02 09:31:56 +08:00
|
|
|
"""
|
|
|
|
from django.db import models
|
|
|
|
|
2011-10-14 02:04:12 +08:00
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
class Publication(models.Model):
|
2007-08-05 13:14:46 +08:00
|
|
|
title = models.CharField(max_length=30)
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2012-08-12 18:32:08 +08:00
|
|
|
def __str__(self):
|
2006-05-02 09:31:56 +08:00
|
|
|
return self.title
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
ordering = ('title',)
|
|
|
|
|
2013-11-03 12:36:09 +08:00
|
|
|
|
2015-07-02 16:43:15 +08:00
|
|
|
class Tag(models.Model):
|
|
|
|
id = models.BigAutoField(primary_key=True)
|
|
|
|
name = models.CharField(max_length=50)
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.name
|
|
|
|
|
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
class Article(models.Model):
|
2007-08-05 13:14:46 +08:00
|
|
|
headline = models.CharField(max_length=100)
|
2017-01-21 05:04:05 +08:00
|
|
|
# Assign a string as name to make sure the intermediary model is
|
2013-04-06 01:59:15 +08:00
|
|
|
# correctly created. Refs #20207
|
|
|
|
publications = models.ManyToManyField(Publication, name='publications')
|
2015-07-02 16:43:15 +08:00
|
|
|
tags = models.ManyToManyField(Tag, related_name='tags')
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2012-08-12 18:32:08 +08:00
|
|
|
def __str__(self):
|
2006-05-02 09:31:56 +08:00
|
|
|
return self.headline
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
ordering = ('headline',)
|
2015-05-09 18:57:13 +08:00
|
|
|
|
|
|
|
|
|
|
|
# Models to test correct related_name inheritance
|
|
|
|
class AbstractArticle(models.Model):
|
|
|
|
class Meta:
|
|
|
|
abstract = True
|
|
|
|
|
|
|
|
publications = models.ManyToManyField(Publication, name='publications', related_name='+')
|
|
|
|
|
|
|
|
|
|
|
|
class InheritedArticleA(AbstractArticle):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class InheritedArticleB(AbstractArticle):
|
|
|
|
pass
|
2018-10-15 23:01:57 +08:00
|
|
|
|
|
|
|
|
|
|
|
class NullableTargetArticle(models.Model):
|
|
|
|
headline = models.CharField(max_length=100)
|
|
|
|
publications = models.ManyToManyField(Publication, through='NullablePublicationThrough')
|
|
|
|
|
|
|
|
|
|
|
|
class NullablePublicationThrough(models.Model):
|
|
|
|
article = models.ForeignKey(NullableTargetArticle, models.CASCADE)
|
|
|
|
publication = models.ForeignKey(Publication, models.CASCADE, null=True)
|