2006-05-02 01:31:56 +00:00
|
|
|
"""
|
2014-09-24 05:13:13 +00:00
|
|
|
Many-to-many relationships
|
2006-05-02 01:31:56 +00:00
|
|
|
|
2008-08-12 14:15:38 +00:00
|
|
|
To define a many-to-many relationship, use ``ManyToManyField()``.
|
2006-05-02 01:31:56 +00:00
|
|
|
|
2008-08-12 14:15:38 +00:00
|
|
|
In this example, an ``Article`` can be published in multiple ``Publication``
|
|
|
|
objects, and a ``Publication`` has multiple ``Article`` objects.
|
2006-05-02 01:31:56 +00:00
|
|
|
"""
|
|
|
|
from django.db import models
|
|
|
|
|
2011-10-13 18:04:12 +00:00
|
|
|
|
2006-05-02 01:31:56 +00:00
|
|
|
class Publication(models.Model):
|
2007-08-05 05:14:46 +00:00
|
|
|
title = models.CharField(max_length=30)
|
2006-05-02 01:31:56 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
ordering = ('title',)
|
|
|
|
|
2018-12-28 00:34:14 +00:00
|
|
|
def __str__(self):
|
|
|
|
return self.title
|
|
|
|
|
2013-11-03 04:36:09 +00:00
|
|
|
|
2015-07-02 08:43:15 +00: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 09:03:53 +00:00
|
|
|
class NoDeletedArticleManager(models.Manager):
|
|
|
|
def get_queryset(self):
|
|
|
|
return super().get_queryset().exclude(headline='deleted')
|
|
|
|
|
|
|
|
|
2006-05-02 01:31:56 +00:00
|
|
|
class Article(models.Model):
|
2007-08-05 05:14:46 +00:00
|
|
|
headline = models.CharField(max_length=100)
|
2017-01-20 21:04:05 +00:00
|
|
|
# Assign a string as name to make sure the intermediary model is
|
2013-04-05 17:59:15 +00:00
|
|
|
# correctly created. Refs #20207
|
|
|
|
publications = models.ManyToManyField(Publication, name='publications')
|
2015-07-02 08:43:15 +00:00
|
|
|
tags = models.ManyToManyField(Tag, related_name='tags')
|
2019-05-03 17:47:53 +00:00
|
|
|
authors = models.ManyToManyField('User', through='UserArticle')
|
2006-05-02 01:31:56 +00:00
|
|
|
|
2019-04-15 09:03:53 +00:00
|
|
|
objects = NoDeletedArticleManager()
|
|
|
|
|
2006-05-02 01:31:56 +00:00
|
|
|
class Meta:
|
|
|
|
ordering = ('headline',)
|
2015-05-09 10:57:13 +00:00
|
|
|
|
2018-12-28 00:34:14 +00:00
|
|
|
def __str__(self):
|
|
|
|
return self.headline
|
|
|
|
|
2015-05-09 10:57:13 +00:00
|
|
|
|
2019-05-03 17:47:53 +00: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 10:57:13 +00: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
|