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
|
|
|
|
|
|
|
class Meta:
|
|
|
|
ordering = ("title",)
|
|
|
|
|
2018-12-28 08:34:14 +08:00
|
|
|
def __str__(self):
|
|
|
|
return self.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
|
|
|
|
|
|
|
|
|
2019-04-15 17:03:53 +08:00
|
|
|
class NoDeletedArticleManager(models.Manager):
|
|
|
|
def get_queryset(self):
|
|
|
|
return super().get_queryset().exclude(headline="deleted")
|
|
|
|
|
|
|
|
|
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")
|
2019-05-04 01:47:53 +08:00
|
|
|
authors = models.ManyToManyField("User", through="UserArticle")
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2019-04-15 17:03:53 +08:00
|
|
|
objects = NoDeletedArticleManager()
|
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
class Meta:
|
|
|
|
ordering = ("headline",)
|
2015-05-09 18:57:13 +08:00
|
|
|
|
2018-12-28 08:34:14 +08:00
|
|
|
def __str__(self):
|
|
|
|
return self.headline
|
|
|
|
|
2015-05-09 18:57:13 +08:00
|
|
|
|
2019-05-04 01:47:53 +08:00
|
|
|
class User(models.Model):
|
|
|
|
username = models.CharField(max_length=20, unique=True)
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.username
|
|
|
|
|
|
|
|
|
|
|
|
class UserArticle(models.Model):
|
|
|
|
user = models.ForeignKey(User, models.CASCADE, to_field="username")
|
|
|
|
article = models.ForeignKey(Article, models.CASCADE)
|
|
|
|
|
|
|
|
|
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
|