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