2015-06-05 13:40:51 +02:00
|
|
|
import uuid
|
|
|
|
|
2014-01-22 07:43:33 +01:00
|
|
|
from django.contrib.contenttypes.fields import GenericForeignKey, GenericRelation
|
2011-10-13 20:04:12 +02:00
|
|
|
from django.contrib.contenttypes.models import ContentType
|
2011-10-06 01:14:52 +02:00
|
|
|
from django.db import models
|
2019-08-20 09:54:41 +02:00
|
|
|
from django.db.models.query import ModelIterable
|
2016-07-19 20:55:59 +02:00
|
|
|
from django.utils.functional import cached_property
|
2011-10-06 01:14:52 +02:00
|
|
|
|
|
|
|
|
|
|
|
class Author(models.Model):
|
|
|
|
name = models.CharField(max_length=50, unique=True)
|
2015-07-22 16:43:21 +02:00
|
|
|
first_book = models.ForeignKey(
|
|
|
|
"Book", models.CASCADE, related_name="first_time_authors"
|
|
|
|
)
|
2011-10-06 01:14:52 +02:00
|
|
|
favorite_authors = models.ManyToManyField(
|
|
|
|
"self", through="FavoriteAuthors", symmetrical=False, related_name="favors_me"
|
|
|
|
)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
ordering = ["id"]
|
|
|
|
|
2018-12-28 01:34:14 +01:00
|
|
|
def __str__(self):
|
|
|
|
return self.name
|
|
|
|
|
2011-10-06 01:14:52 +02:00
|
|
|
|
|
|
|
class AuthorWithAge(Author):
|
2015-07-22 16:43:21 +02:00
|
|
|
author = models.OneToOneField(Author, models.CASCADE, parent_link=True)
|
2011-10-06 01:14:52 +02:00
|
|
|
age = models.IntegerField()
|
|
|
|
|
|
|
|
|
|
|
|
class FavoriteAuthors(models.Model):
|
2015-07-22 16:43:21 +02:00
|
|
|
author = models.ForeignKey(
|
|
|
|
Author, models.CASCADE, to_field="name", related_name="i_like"
|
|
|
|
)
|
|
|
|
likes_author = models.ForeignKey(
|
|
|
|
Author, models.CASCADE, to_field="name", related_name="likes_me"
|
|
|
|
)
|
2011-10-06 01:14:52 +02:00
|
|
|
|
|
|
|
class Meta:
|
2013-09-03 20:22:21 +02:00
|
|
|
ordering = ["id"]
|
2011-10-06 01:14:52 +02:00
|
|
|
|
|
|
|
|
|
|
|
class AuthorAddress(models.Model):
|
2015-07-22 16:43:21 +02:00
|
|
|
author = models.ForeignKey(
|
|
|
|
Author, models.CASCADE, to_field="name", related_name="addresses"
|
|
|
|
)
|
2011-10-06 01:14:52 +02:00
|
|
|
address = models.TextField()
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
ordering = ["id"]
|
|
|
|
|
|
|
|
|
|
|
|
class Book(models.Model):
|
|
|
|
title = models.CharField(max_length=255)
|
|
|
|
authors = models.ManyToManyField(Author, related_name="books")
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
ordering = ["id"]
|
|
|
|
|
2013-11-02 22:34:05 +01:00
|
|
|
|
2011-10-06 01:14:52 +02:00
|
|
|
class BookWithYear(Book):
|
2015-07-22 16:43:21 +02:00
|
|
|
book = models.OneToOneField(Book, models.CASCADE, parent_link=True)
|
2011-10-06 01:14:52 +02:00
|
|
|
published_year = models.IntegerField()
|
|
|
|
aged_authors = models.ManyToManyField(AuthorWithAge, related_name="books_with_year")
|
|
|
|
|
|
|
|
|
2014-05-18 22:43:13 +02:00
|
|
|
class Bio(models.Model):
|
2017-08-14 23:27:25 +02:00
|
|
|
author = models.OneToOneField(
|
|
|
|
Author,
|
|
|
|
models.CASCADE,
|
|
|
|
primary_key=True,
|
|
|
|
to_field="name",
|
|
|
|
)
|
2014-05-18 22:43:13 +02:00
|
|
|
books = models.ManyToManyField(Book, blank=True)
|
|
|
|
|
|
|
|
|
2011-10-06 01:14:52 +02:00
|
|
|
class Reader(models.Model):
|
|
|
|
name = models.CharField(max_length=50)
|
|
|
|
books_read = models.ManyToManyField(Book, related_name="read_by")
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
ordering = ["id"]
|
|
|
|
|
2018-12-28 01:34:14 +01:00
|
|
|
def __str__(self):
|
|
|
|
return self.name
|
|
|
|
|
2013-11-02 22:34:05 +01:00
|
|
|
|
2012-06-06 15:17:32 +02:00
|
|
|
class BookReview(models.Model):
|
2018-02-05 17:02:57 +01:00
|
|
|
# Intentionally does not have a related name.
|
|
|
|
book = models.ForeignKey(BookWithYear, models.CASCADE, null=True)
|
2012-06-06 15:17:32 +02:00
|
|
|
notes = models.TextField(null=True, blank=True)
|
2011-10-06 01:14:52 +02:00
|
|
|
|
|
|
|
|
2015-02-05 19:25:34 +01:00
|
|
|
# Models for default manager tests
|
2013-11-02 22:34:05 +01:00
|
|
|
|
2022-02-03 20:24:19 +01:00
|
|
|
|
2011-10-06 01:14:52 +02:00
|
|
|
class Qualification(models.Model):
|
|
|
|
name = models.CharField(max_length=10)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
ordering = ["id"]
|
|
|
|
|
|
|
|
|
2017-04-19 10:24:22 +02:00
|
|
|
class ModelIterableSubclass(ModelIterable):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2019-08-20 09:54:41 +02:00
|
|
|
class TeacherQuerySet(models.QuerySet):
|
2017-04-19 10:24:22 +02:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
self._iterable_class = ModelIterableSubclass
|
|
|
|
|
|
|
|
|
2011-10-06 01:14:52 +02:00
|
|
|
class TeacherManager(models.Manager):
|
2013-03-08 15:15:23 +01:00
|
|
|
def get_queryset(self):
|
2017-01-21 14:13:44 +01:00
|
|
|
return super().get_queryset().prefetch_related("qualifications")
|
2011-10-06 01:14:52 +02:00
|
|
|
|
|
|
|
|
|
|
|
class Teacher(models.Model):
|
|
|
|
name = models.CharField(max_length=50)
|
|
|
|
qualifications = models.ManyToManyField(Qualification)
|
|
|
|
|
|
|
|
objects = TeacherManager()
|
2017-04-19 10:24:22 +02:00
|
|
|
objects_custom = TeacherQuerySet.as_manager()
|
2011-10-06 01:14:52 +02:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
ordering = ["id"]
|
|
|
|
|
2018-12-28 01:34:14 +01:00
|
|
|
def __str__(self):
|
|
|
|
return "%s (%s)" % (
|
|
|
|
self.name,
|
|
|
|
", ".join(q.name for q in self.qualifications.all()),
|
|
|
|
)
|
|
|
|
|
2011-10-06 01:14:52 +02:00
|
|
|
|
|
|
|
class Department(models.Model):
|
|
|
|
name = models.CharField(max_length=50)
|
|
|
|
teachers = models.ManyToManyField(Teacher)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
ordering = ["id"]
|
|
|
|
|
|
|
|
|
2015-02-05 19:25:34 +01:00
|
|
|
# GenericRelation/GenericForeignKey tests
|
2011-10-06 01:14:52 +02:00
|
|
|
|
2022-02-03 20:24:19 +01:00
|
|
|
|
2011-10-06 01:14:52 +02:00
|
|
|
class TaggedItem(models.Model):
|
|
|
|
tag = models.SlugField()
|
2015-07-22 16:43:21 +02:00
|
|
|
content_type = models.ForeignKey(
|
|
|
|
ContentType,
|
|
|
|
models.CASCADE,
|
|
|
|
related_name="taggeditem_set2",
|
|
|
|
)
|
2011-10-06 01:14:52 +02:00
|
|
|
object_id = models.PositiveIntegerField()
|
2014-01-22 07:43:33 +01:00
|
|
|
content_object = GenericForeignKey("content_type", "object_id")
|
2015-07-22 16:43:21 +02:00
|
|
|
created_by_ct = models.ForeignKey(
|
|
|
|
ContentType,
|
|
|
|
models.SET_NULL,
|
|
|
|
null=True,
|
|
|
|
related_name="taggeditem_set3",
|
|
|
|
)
|
2011-10-07 18:05:53 +02:00
|
|
|
created_by_fkey = models.PositiveIntegerField(null=True)
|
2014-01-22 07:43:33 +01:00
|
|
|
created_by = GenericForeignKey(
|
|
|
|
"created_by_ct",
|
|
|
|
"created_by_fkey",
|
|
|
|
)
|
2015-07-22 16:43:21 +02:00
|
|
|
favorite_ct = models.ForeignKey(
|
|
|
|
ContentType,
|
|
|
|
models.SET_NULL,
|
|
|
|
null=True,
|
|
|
|
related_name="taggeditem_set4",
|
|
|
|
)
|
2012-10-29 14:40:32 +01:00
|
|
|
favorite_fkey = models.CharField(max_length=64, null=True)
|
2014-01-22 07:43:33 +01:00
|
|
|
favorite = GenericForeignKey("favorite_ct", "favorite_fkey")
|
2011-10-06 01:14:52 +02:00
|
|
|
|
2013-11-06 18:25:05 +01:00
|
|
|
class Meta:
|
|
|
|
ordering = ["id"]
|
|
|
|
|
2011-10-06 01:14:52 +02:00
|
|
|
|
2019-04-14 10:02:59 +02:00
|
|
|
class Article(models.Model):
|
|
|
|
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
|
|
|
|
name = models.CharField(max_length=20)
|
|
|
|
|
|
|
|
|
2011-10-06 01:14:52 +02:00
|
|
|
class Bookmark(models.Model):
|
|
|
|
url = models.URLField()
|
2014-03-04 12:23:32 +01:00
|
|
|
tags = GenericRelation(TaggedItem, related_query_name="bookmarks")
|
2014-01-22 07:43:33 +01:00
|
|
|
favorite_tags = GenericRelation(
|
|
|
|
TaggedItem,
|
2012-10-29 14:40:32 +01:00
|
|
|
content_type_field="favorite_ct",
|
|
|
|
object_id_field="favorite_fkey",
|
2014-03-04 12:23:32 +01:00
|
|
|
related_query_name="favorite_bookmarks",
|
|
|
|
)
|
2011-10-06 01:14:52 +02:00
|
|
|
|
2013-11-06 18:25:05 +01:00
|
|
|
class Meta:
|
|
|
|
ordering = ["id"]
|
|
|
|
|
2011-10-06 01:14:52 +02:00
|
|
|
|
2012-03-15 16:06:57 +01:00
|
|
|
class Comment(models.Model):
|
|
|
|
comment = models.TextField()
|
|
|
|
|
|
|
|
# Content-object field
|
2019-04-14 10:02:59 +02:00
|
|
|
content_type = models.ForeignKey(ContentType, models.CASCADE, null=True)
|
2013-10-22 15:31:43 +02:00
|
|
|
object_pk = models.TextField()
|
2014-01-22 07:43:33 +01:00
|
|
|
content_object = GenericForeignKey(ct_field="content_type", fk_field="object_pk")
|
2019-04-14 10:02:59 +02:00
|
|
|
content_type_uuid = models.ForeignKey(
|
|
|
|
ContentType, models.CASCADE, related_name="comments", null=True
|
|
|
|
)
|
|
|
|
object_pk_uuid = models.TextField()
|
|
|
|
content_object_uuid = GenericForeignKey(
|
|
|
|
ct_field="content_type_uuid", fk_field="object_pk_uuid"
|
|
|
|
)
|
2012-03-15 16:06:57 +01:00
|
|
|
|
2013-11-06 18:25:05 +01:00
|
|
|
class Meta:
|
|
|
|
ordering = ["id"]
|
|
|
|
|
2012-03-15 16:06:57 +01:00
|
|
|
|
2015-02-05 19:25:34 +01:00
|
|
|
# Models for lookup ordering tests
|
2011-10-06 01:14:52 +02:00
|
|
|
|
2022-02-03 20:24:19 +01:00
|
|
|
|
2011-10-06 01:14:52 +02:00
|
|
|
class House(models.Model):
|
2014-01-16 19:24:39 +01:00
|
|
|
name = models.CharField(max_length=50)
|
2011-10-06 01:14:52 +02:00
|
|
|
address = models.CharField(max_length=255)
|
2015-07-22 16:43:21 +02:00
|
|
|
owner = models.ForeignKey("Person", models.SET_NULL, null=True)
|
|
|
|
main_room = models.OneToOneField(
|
|
|
|
"Room", models.SET_NULL, related_name="main_room_of", null=True
|
|
|
|
)
|
2011-10-06 01:14:52 +02:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
ordering = ["id"]
|
|
|
|
|
2013-11-02 22:34:05 +01:00
|
|
|
|
2011-10-06 01:14:52 +02:00
|
|
|
class Room(models.Model):
|
|
|
|
name = models.CharField(max_length=50)
|
2015-07-22 16:43:21 +02:00
|
|
|
house = models.ForeignKey(House, models.CASCADE, related_name="rooms")
|
2011-10-06 01:14:52 +02:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
ordering = ["id"]
|
|
|
|
|
|
|
|
|
|
|
|
class Person(models.Model):
|
|
|
|
name = models.CharField(max_length=50)
|
|
|
|
houses = models.ManyToManyField(House, related_name="occupants")
|
|
|
|
|
|
|
|
@property
|
|
|
|
def primary_house(self):
|
|
|
|
# Assume business logic forces every person to have at least one house.
|
|
|
|
return sorted(self.houses.all(), key=lambda house: -house.rooms.count())[0]
|
|
|
|
|
2014-05-18 22:43:13 +02:00
|
|
|
@property
|
|
|
|
def all_houses(self):
|
|
|
|
return list(self.houses.all())
|
|
|
|
|
2016-07-19 20:55:59 +02:00
|
|
|
@cached_property
|
|
|
|
def cached_all_houses(self):
|
|
|
|
return self.all_houses
|
|
|
|
|
2011-10-06 01:14:52 +02:00
|
|
|
class Meta:
|
|
|
|
ordering = ["id"]
|
|
|
|
|
|
|
|
|
2015-02-05 19:25:34 +01:00
|
|
|
# Models for nullable FK tests
|
2011-10-06 01:14:52 +02:00
|
|
|
|
2022-02-03 20:24:19 +01:00
|
|
|
|
2011-10-06 01:14:52 +02:00
|
|
|
class Employee(models.Model):
|
|
|
|
name = models.CharField(max_length=50)
|
2015-07-22 16:43:21 +02:00
|
|
|
boss = models.ForeignKey("self", models.SET_NULL, null=True, related_name="serfs")
|
2011-10-06 01:14:52 +02:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
ordering = ["id"]
|
2013-05-24 11:01:34 +02:00
|
|
|
|
|
|
|
|
2015-02-05 19:25:34 +01:00
|
|
|
# Ticket #19607
|
2013-05-24 11:01:34 +02:00
|
|
|
|
2022-02-03 20:24:19 +01:00
|
|
|
|
2013-05-24 11:01:34 +02:00
|
|
|
class LessonEntry(models.Model):
|
|
|
|
name1 = models.CharField(max_length=200)
|
|
|
|
name2 = models.CharField(max_length=200)
|
|
|
|
|
|
|
|
|
|
|
|
class WordEntry(models.Model):
|
2015-07-22 16:43:21 +02:00
|
|
|
lesson_entry = models.ForeignKey(LessonEntry, models.CASCADE)
|
2013-05-24 11:01:34 +02:00
|
|
|
name = models.CharField(max_length=200)
|
|
|
|
|
2013-11-13 05:42:12 +01:00
|
|
|
|
2015-02-05 19:25:34 +01:00
|
|
|
# Ticket #21410: Regression when related_name="+"
|
2013-11-13 05:42:12 +01:00
|
|
|
|
2022-02-03 20:24:19 +01:00
|
|
|
|
2013-11-13 05:42:12 +01:00
|
|
|
class Author2(models.Model):
|
|
|
|
name = models.CharField(max_length=50, unique=True)
|
2015-07-22 16:43:21 +02:00
|
|
|
first_book = models.ForeignKey(
|
|
|
|
"Book", models.CASCADE, related_name="first_time_authors+"
|
|
|
|
)
|
2013-11-13 05:42:12 +01:00
|
|
|
favorite_books = models.ManyToManyField("Book", related_name="+")
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
ordering = ["id"]
|
2015-06-05 13:40:51 +02:00
|
|
|
|
|
|
|
|
|
|
|
# Models for many-to-many with UUID pk test:
|
|
|
|
|
2022-02-03 20:24:19 +01:00
|
|
|
|
2015-06-05 13:40:51 +02:00
|
|
|
class Pet(models.Model):
|
|
|
|
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
|
|
|
|
name = models.CharField(max_length=20)
|
|
|
|
people = models.ManyToManyField(Person, related_name="pets")
|
|
|
|
|
|
|
|
|
|
|
|
class Flea(models.Model):
|
|
|
|
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
|
2015-07-22 16:43:21 +02:00
|
|
|
current_room = models.ForeignKey(
|
|
|
|
Room, models.SET_NULL, related_name="fleas", null=True
|
|
|
|
)
|
2015-06-05 13:40:51 +02:00
|
|
|
pets_visited = models.ManyToManyField(Pet, related_name="fleas_hosted")
|
|
|
|
people_visited = models.ManyToManyField(Person, related_name="fleas_hosted")
|