| 1 |
import uuid |
| 2 |
|
| 3 |
from django.contrib.contenttypes.fields import GenericForeignKey, GenericRelation |
| 4 |
from django.contrib.contenttypes.models import ContentType |
| 5 |
from django.db import models |
| 6 |
from django.db.models.query import ModelIterable |
| 7 |
from django.utils.functional import cached_property |
| 8 |
|
| 9 |
|
| 10 |
class Author(models.Model): |
| 11 |
name = models.CharField(max_length=50, unique=True) |
| 12 |
first_book = models.ForeignKey( |
| 13 |
"Book", models.CASCADE, related_name="first_time_authors" |
| 14 |
) |
| 15 |
favorite_authors = models.ManyToManyField( |
| 16 |
"self", through="FavoriteAuthors", symmetrical=False, related_name="favors_me" |
| 17 |
) |
| 18 |
|
| 19 |
class Meta: |
| 20 |
ordering = ["id"] |
| 21 |
|
| 22 |
def __str__(self): |
| 23 |
return self.name |
| 24 |
|
| 25 |
|
| 26 |
class AuthorWithAge(Author): |
| 27 |
author = models.OneToOneField(Author, models.CASCADE, parent_link=True) |
| 28 |
age = models.IntegerField() |
| 29 |
|
| 30 |
|
| 31 |
class FavoriteAuthors(models.Model): |
| 32 |
author = models.ForeignKey( |
| 33 |
Author, models.CASCADE, to_field="name", related_name="i_like" |
| 34 |
) |
| 35 |
likes_author = models.ForeignKey( |
| 36 |
Author, models.CASCADE, to_field="name", related_name="likes_me" |
| 37 |
) |
| 38 |
is_active = models.BooleanField(default=True) |
| 39 |
|
| 40 |
class Meta: |
| 41 |
ordering = ["id"] |
| 42 |
|
| 43 |
|
| 44 |
class AuthorAddress(models.Model): |
| 45 |
author = models.ForeignKey( |
| 46 |
Author, models.CASCADE, to_field="name", related_name="addresses" |
| 47 |
) |
| 48 |
address = models.TextField() |
| 49 |
|
| 50 |
class Meta: |
| 51 |
ordering = ["id"] |
| 52 |
|
| 53 |
|
| 54 |
class Book(models.Model): |
| 55 |
title = models.CharField(max_length=255) |
| 56 |
authors = models.ManyToManyField(Author, related_name="books") |
| 57 |
|
| 58 |
class Meta: |
| 59 |
ordering = ["id"] |
| 60 |
|
| 61 |
|
| 62 |
class BookWithYear(Book): |
| 63 |
book = models.OneToOneField(Book, models.CASCADE, parent_link=True) |
| 64 |
published_year = models.IntegerField() |
| 65 |
aged_authors = models.ManyToManyField(AuthorWithAge, related_name="books_with_year") |
| 66 |
|
| 67 |
|
| 68 |
class Bio(models.Model): |
| 69 |
author = models.OneToOneField( |
| 70 |
Author, |
| 71 |
models.CASCADE, |
| 72 |
primary_key=True, |
| 73 |
to_field="name", |
| 74 |
) |
| 75 |
books = models.ManyToManyField(Book, blank=True) |
| 76 |
|
| 77 |
|
| 78 |
class Reader(models.Model): |
| 79 |
name = models.CharField(max_length=50) |
| 80 |
books_read = models.ManyToManyField(Book, related_name="read_by") |
| 81 |
|
| 82 |
class Meta: |
| 83 |
ordering = ["id"] |
| 84 |
|
| 85 |
def __str__(self): |
| 86 |
return self.name |
| 87 |
|
| 88 |
|
| 89 |
class BookReview(models.Model): |
| 90 |
# Intentionally does not have a related name. |
| 91 |
book = models.ForeignKey(BookWithYear, models.CASCADE, null=True) |
| 92 |
notes = models.TextField(null=True, blank=True) |
| 93 |
|
| 94 |
|
| 95 |
# Models for default manager tests |
| 96 |
|
| 97 |
|
| 98 |
class Qualification(models.Model): |
| 99 |
name = models.CharField(max_length=10) |
| 100 |
|
| 101 |
class Meta: |
| 102 |
ordering = ["id"] |
| 103 |
|
| 104 |
|
| 105 |
class ModelIterableSubclass(ModelIterable): |
| 106 |
pass |
| 107 |
|
| 108 |
|
| 109 |
class TeacherQuerySet(models.QuerySet): |
| 110 |
def __init__(self, *args, **kwargs): |
| 111 |
super().__init__(*args, **kwargs) |
| 112 |
self._iterable_class = ModelIterableSubclass |
| 113 |
|
| 114 |
|
| 115 |
class TeacherManager(models.Manager): |
| 116 |
def get_queryset(self): |
| 117 |
return super().get_queryset().prefetch_related("qualifications") |
| 118 |
|
| 119 |
|
| 120 |
class Teacher(models.Model): |
| 121 |
name = models.CharField(max_length=50) |
| 122 |
qualifications = models.ManyToManyField(Qualification) |
| 123 |
|
| 124 |
objects = TeacherManager() |
| 125 |
objects_custom = TeacherQuerySet.as_manager() |
| 126 |
|
| 127 |
class Meta: |
| 128 |
ordering = ["id"] |
| 129 |
|
| 130 |
def __str__(self): |
| 131 |
return "%s (%s)" % ( |
| 132 |
self.name, |
| 133 |
", ".join(q.name for q in self.qualifications.all()), |
| 134 |
) |
| 135 |
|
| 136 |
|
| 137 |
class Department(models.Model): |
| 138 |
name = models.CharField(max_length=50) |
| 139 |
teachers = models.ManyToManyField(Teacher) |
| 140 |
|
| 141 |
class Meta: |
| 142 |
ordering = ["id"] |
| 143 |
|
| 144 |
|
| 145 |
# GenericRelation/GenericForeignKey tests |
| 146 |
|
| 147 |
|
| 148 |
class TaggedItem(models.Model): |
| 149 |
tag = models.SlugField() |
| 150 |
content_type = models.ForeignKey( |
| 151 |
ContentType, |
| 152 |
models.CASCADE, |
| 153 |
related_name="taggeditem_set2", |
| 154 |
) |
| 155 |
object_id = models.PositiveIntegerField() |
| 156 |
content_object = GenericForeignKey("content_type", "object_id") |
| 157 |
created_by_ct = models.ForeignKey( |
| 158 |
ContentType, |
| 159 |
models.SET_NULL, |
| 160 |
null=True, |
| 161 |
related_name="taggeditem_set3", |
| 162 |
) |
| 163 |
created_by_fkey = models.PositiveIntegerField(null=True) |
| 164 |
created_by = GenericForeignKey( |
| 165 |
"created_by_ct", |
| 166 |
"created_by_fkey", |
| 167 |
) |
| 168 |
favorite_ct = models.ForeignKey( |
| 169 |
ContentType, |
| 170 |
models.SET_NULL, |
| 171 |
null=True, |
| 172 |
related_name="taggeditem_set4", |
| 173 |
) |
| 174 |
favorite_fkey = models.CharField(max_length=64, null=True) |
| 175 |
favorite = GenericForeignKey("favorite_ct", "favorite_fkey") |
| 176 |
|
| 177 |
class Meta: |
| 178 |
ordering = ["id"] |
| 179 |
|
| 180 |
|
| 181 |
class Article(models.Model): |
| 182 |
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False) |
| 183 |
name = models.CharField(max_length=20) |
| 184 |
|
| 185 |
|
| 186 |
class Bookmark(models.Model): |
| 187 |
url = models.URLField() |
| 188 |
tags = GenericRelation(TaggedItem, related_query_name="bookmarks") |
| 189 |
favorite_tags = GenericRelation( |
| 190 |
TaggedItem, |
| 191 |
content_type_field="favorite_ct", |
| 192 |
object_id_field="favorite_fkey", |
| 193 |
related_query_name="favorite_bookmarks", |
| 194 |
) |
| 195 |
|
| 196 |
class Meta: |
| 197 |
ordering = ["id"] |
| 198 |
|
| 199 |
|
| 200 |
class Comment(models.Model): |
| 201 |
comment = models.TextField() |
| 202 |
|
| 203 |
# Content-object field |
| 204 |
content_type = models.ForeignKey(ContentType, models.CASCADE, null=True) |
| 205 |
object_pk = models.TextField() |
| 206 |
content_object = GenericForeignKey(ct_field="content_type", fk_field="object_pk") |
| 207 |
content_type_uuid = models.ForeignKey( |
| 208 |
ContentType, models.CASCADE, related_name="comments", null=True |
| 209 |
) |
| 210 |
object_pk_uuid = models.TextField() |
| 211 |
content_object_uuid = GenericForeignKey( |
| 212 |
ct_field="content_type_uuid", fk_field="object_pk_uuid" |
| 213 |
) |
| 214 |
|
| 215 |
class Meta: |
| 216 |
ordering = ["id"] |
| 217 |
|
| 218 |
|
| 219 |
# Models for lookup ordering tests |
| 220 |
|
| 221 |
|
| 222 |
class House(models.Model): |
| 223 |
name = models.CharField(max_length=50) |
| 224 |
address = models.CharField(max_length=255) |
| 225 |
owner = models.ForeignKey("Person", models.SET_NULL, null=True) |
| 226 |
main_room = models.OneToOneField( |
| 227 |
"Room", models.SET_NULL, related_name="main_room_of", null=True |
| 228 |
) |
| 229 |
|
| 230 |
class Meta: |
| 231 |
ordering = ["id"] |
| 232 |
|
| 233 |
|
| 234 |
class Room(models.Model): |
| 235 |
name = models.CharField(max_length=50) |
| 236 |
house = models.ForeignKey(House, models.CASCADE, related_name="rooms") |
| 237 |
|
| 238 |
class Meta: |
| 239 |
ordering = ["id"] |
| 240 |
|
| 241 |
|
| 242 |
class Person(models.Model): |
| 243 |
name = models.CharField(max_length=50) |
| 244 |
houses = models.ManyToManyField(House, related_name="occupants") |
| 245 |
|
| 246 |
@property |
| 247 |
def primary_house(self): |
| 248 |
# Assume business logic forces every person to have at least one house. |
| 249 |
return sorted(self.houses.all(), key=lambda house: -house.rooms.count())[0] |
| 250 |
|
| 251 |
@property |
| 252 |
def all_houses(self): |
| 253 |
return list(self.houses.all()) |
| 254 |
|
| 255 |
@cached_property |
| 256 |
def cached_all_houses(self): |
| 257 |
return self.all_houses |
| 258 |
|
| 259 |
class Meta: |
| 260 |
ordering = ["id"] |
| 261 |
|
| 262 |
|
| 263 |
# Models for nullable FK tests |
| 264 |
|
| 265 |
|
| 266 |
class Employee(models.Model): |
| 267 |
name = models.CharField(max_length=50) |
| 268 |
boss = models.ForeignKey("self", models.SET_NULL, null=True, related_name="serfs") |
| 269 |
|
| 270 |
class Meta: |
| 271 |
ordering = ["id"] |
| 272 |
|
| 273 |
|
| 274 |
class SelfDirectedEmployee(Employee): |
| 275 |
pass |
| 276 |
|
| 277 |
|
| 278 |
# Ticket #19607 |
| 279 |
|
| 280 |
|
| 281 |
class LessonEntry(models.Model): |
| 282 |
name1 = models.CharField(max_length=200) |
| 283 |
name2 = models.CharField(max_length=200) |
| 284 |
|
| 285 |
|
| 286 |
class WordEntry(models.Model): |
| 287 |
lesson_entry = models.ForeignKey(LessonEntry, models.CASCADE) |
| 288 |
name = models.CharField(max_length=200) |
| 289 |
|
| 290 |
|
| 291 |
# Ticket #21410: Regression when related_name="+" |
| 292 |
|
| 293 |
|
| 294 |
class Author2(models.Model): |
| 295 |
name = models.CharField(max_length=50, unique=True) |
| 296 |
first_book = models.ForeignKey( |
| 297 |
"Book", models.CASCADE, related_name="first_time_authors+" |
| 298 |
) |
| 299 |
favorite_books = models.ManyToManyField("Book", related_name="+") |
| 300 |
|
| 301 |
class Meta: |
| 302 |
ordering = ["id"] |
| 303 |
|
| 304 |
|
| 305 |
# Models for many-to-many with UUID pk test: |
| 306 |
|
| 307 |
|
| 308 |
class Pet(models.Model): |
| 309 |
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False) |
| 310 |
name = models.CharField(max_length=20) |
| 311 |
people = models.ManyToManyField(Person, related_name="pets") |
| 312 |
|
| 313 |
|
| 314 |
class Flea(models.Model): |
| 315 |
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False) |
| 316 |
current_room = models.ForeignKey( |
| 317 |
Room, models.SET_NULL, related_name="fleas", null=True |
| 318 |
) |
| 319 |
pets_visited = models.ManyToManyField(Pet, related_name="fleas_hosted") |
| 320 |
people_visited = models.ManyToManyField(Person, related_name="fleas_hosted") |