| 1 |
import uuid |
| 2 |
|
| 3 |
from django.contrib.auth.models import Group |
| 4 |
from django.contrib.auth import get_user_model |
| 5 |
from django.contrib.contenttypes.models import ContentType |
| 6 |
from django.db.models import Manager |
| 7 |
from django.db import models |
| 8 |
from django.db.models.query import QuerySet |
| 9 |
from django.db.models import F |
| 10 |
from django.db.models.functions import Upper |
| 11 |
from django.contrib.contenttypes.fields import GenericForeignKey, GenericRelation |
| 12 |
|
| 13 |
from polymorphic.managers import PolymorphicManager |
| 14 |
from polymorphic.models import PolymorphicModel |
| 15 |
from polymorphic.query import PolymorphicQuerySet |
| 16 |
from polymorphic.showfields import ShowFieldContent, ShowFieldType, ShowFieldTypeAndContent |
| 17 |
|
| 18 |
|
| 19 |
class PlainA(models.Model): |
| 20 |
field1 = models.CharField(max_length=30) |
| 21 |
|
| 22 |
def __str__(self): |
| 23 |
return self.field1 |
| 24 |
|
| 25 |
|
| 26 |
class PlainB(PlainA): |
| 27 |
field2 = models.CharField(max_length=30) |
| 28 |
|
| 29 |
|
| 30 |
class PlainC(PlainB): |
| 31 |
field3 = models.CharField(max_length=30) |
| 32 |
|
| 33 |
|
| 34 |
class PlainD(PlainA): |
| 35 |
field2 = models.CharField(max_length=30) |
| 36 |
|
| 37 |
|
| 38 |
class Model2A(ShowFieldType, PolymorphicModel): |
| 39 |
field1 = models.CharField(max_length=30) |
| 40 |
polymorphic_showfield_deferred = True |
| 41 |
|
| 42 |
|
| 43 |
class RandomMixinB: |
| 44 |
def random_method(self): |
| 45 |
return "random b" |
| 46 |
|
| 47 |
|
| 48 |
class Model2B(RandomMixinB, Model2A): |
| 49 |
field2 = models.CharField(max_length=30) |
| 50 |
|
| 51 |
|
| 52 |
class RandomMixinC: |
| 53 |
def random_method(self): |
| 54 |
return "random c" |
| 55 |
|
| 56 |
|
| 57 |
class Model2C(RandomMixinC, Model2B): |
| 58 |
field3 = models.CharField(max_length=30, blank=True, default="") |
| 59 |
|
| 60 |
|
| 61 |
class Model2D(Model2C): |
| 62 |
field4 = models.CharField(max_length=30) |
| 63 |
|
| 64 |
|
| 65 |
class ModelExtraA(ShowFieldTypeAndContent, PolymorphicModel): |
| 66 |
field1 = models.CharField(max_length=30) |
| 67 |
|
| 68 |
|
| 69 |
class ModelExtraB(ModelExtraA): |
| 70 |
field2 = models.CharField(max_length=30) |
| 71 |
|
| 72 |
|
| 73 |
class ModelExtraC(ModelExtraB): |
| 74 |
field3 = models.CharField(max_length=30) |
| 75 |
|
| 76 |
|
| 77 |
class ModelExtraExternal(models.Model): |
| 78 |
topic = models.CharField(max_length=30) |
| 79 |
|
| 80 |
|
| 81 |
class ModelShow1(ShowFieldType, PolymorphicModel): |
| 82 |
field1 = models.CharField(max_length=30) |
| 83 |
m2m = models.ManyToManyField("self") |
| 84 |
|
| 85 |
|
| 86 |
class ModelShow2(ShowFieldContent, PolymorphicModel): |
| 87 |
field1 = models.CharField(max_length=30) |
| 88 |
m2m = models.ManyToManyField("self") |
| 89 |
|
| 90 |
|
| 91 |
class ModelShow3(ShowFieldTypeAndContent, PolymorphicModel): |
| 92 |
field1 = models.CharField(max_length=30) |
| 93 |
m2m = models.ManyToManyField("self") |
| 94 |
|
| 95 |
|
| 96 |
class ModelShow1_plain(PolymorphicModel): |
| 97 |
field1 = models.CharField(max_length=30) |
| 98 |
|
| 99 |
|
| 100 |
class ModelShow2_plain(ModelShow1_plain): |
| 101 |
field2 = models.CharField(max_length=30) |
| 102 |
|
| 103 |
|
| 104 |
class Base(ShowFieldType, PolymorphicModel): |
| 105 |
polymorphic_showfield_deferred = True |
| 106 |
field_b = models.CharField(max_length=30) |
| 107 |
|
| 108 |
|
| 109 |
class ModelX(Base): |
| 110 |
field_x = models.CharField(max_length=30) |
| 111 |
|
| 112 |
|
| 113 |
class ModelY(Base): |
| 114 |
field_y = models.CharField(max_length=30) |
| 115 |
|
| 116 |
|
| 117 |
class Enhance_Plain(models.Model): |
| 118 |
field_p = models.CharField(max_length=30) |
| 119 |
|
| 120 |
|
| 121 |
class Enhance_Base(ShowFieldTypeAndContent, PolymorphicModel): |
| 122 |
base_id = models.AutoField(primary_key=True) |
| 123 |
field_b = models.CharField(max_length=30) |
| 124 |
|
| 125 |
|
| 126 |
class Enhance_Inherit(Enhance_Base, Enhance_Plain): |
| 127 |
field_i = models.CharField(max_length=30) |
| 128 |
|
| 129 |
|
| 130 |
class RelationAbstractModel(models.Model): |
| 131 |
class Meta: |
| 132 |
abstract = True |
| 133 |
|
| 134 |
|
| 135 |
class RelationBase(RelationAbstractModel, ShowFieldTypeAndContent, PolymorphicModel): |
| 136 |
field_base = models.CharField(max_length=30) |
| 137 |
fk = models.ForeignKey( |
| 138 |
"self", on_delete=models.CASCADE, null=True, related_name="relationbase_set" |
| 139 |
) |
| 140 |
m2m = models.ManyToManyField("self") |
| 141 |
|
| 142 |
|
| 143 |
class RelationA(RelationBase): |
| 144 |
field_a = models.CharField(max_length=30) |
| 145 |
|
| 146 |
|
| 147 |
class RelationB(RelationBase): |
| 148 |
field_b = models.CharField(max_length=30) |
| 149 |
|
| 150 |
|
| 151 |
class RelationBC(RelationB): |
| 152 |
field_c = models.CharField(max_length=30) |
| 153 |
|
| 154 |
|
| 155 |
class RelatingModel(models.Model): |
| 156 |
many2many = models.ManyToManyField(Model2A) |
| 157 |
|
| 158 |
|
| 159 |
class One2OneRelatingModel(PolymorphicModel): |
| 160 |
one2one = models.OneToOneField(Model2A, on_delete=models.CASCADE) |
| 161 |
field1 = models.CharField(max_length=30) |
| 162 |
|
| 163 |
|
| 164 |
class One2OneRelatingModelDerived(One2OneRelatingModel): |
| 165 |
field2 = models.CharField(max_length=30) |
| 166 |
|
| 167 |
|
| 168 |
class ModelUnderRelParent(PolymorphicModel): |
| 169 |
field1 = models.CharField(max_length=30) |
| 170 |
_private = models.CharField(max_length=30) |
| 171 |
|
| 172 |
|
| 173 |
class ModelUnderRelChild(PolymorphicModel): |
| 174 |
parent = models.ForeignKey( |
| 175 |
ModelUnderRelParent, on_delete=models.CASCADE, related_name="children" |
| 176 |
) |
| 177 |
_private2 = models.CharField(max_length=30) |
| 178 |
|
| 179 |
|
| 180 |
class MyManagerQuerySet(PolymorphicQuerySet): |
| 181 |
def my_queryset_foo(self): |
| 182 |
# Just a method to prove the existence of the custom queryset. |
| 183 |
return self.all() |
| 184 |
|
| 185 |
|
| 186 |
class MyManager(PolymorphicManager): |
| 187 |
queryset_class = MyManagerQuerySet |
| 188 |
|
| 189 |
def get_queryset(self): |
| 190 |
return super().get_queryset().order_by("-field1") |
| 191 |
|
| 192 |
def my_queryset_foo(self): |
| 193 |
return self.all().my_queryset_foo() |
| 194 |
|
| 195 |
|
| 196 |
class ModelWithMyManager(ShowFieldTypeAndContent, Model2A): |
| 197 |
objects = MyManager() |
| 198 |
field4 = models.CharField(max_length=30) |
| 199 |
|
| 200 |
|
| 201 |
class ModelWithMyManagerNoDefault(ShowFieldTypeAndContent, Model2A): |
| 202 |
objects = PolymorphicManager() |
| 203 |
my_objects = MyManager() |
| 204 |
field4 = models.CharField(max_length=30) |
| 205 |
|
| 206 |
|
| 207 |
class ModelWithMyManagerDefault(ShowFieldTypeAndContent, Model2A): |
| 208 |
my_objects = MyManager() |
| 209 |
objects = PolymorphicManager() |
| 210 |
field4 = models.CharField(max_length=30) |
| 211 |
|
| 212 |
|
| 213 |
class ModelWithMyManager2(ShowFieldTypeAndContent, Model2A): |
| 214 |
objects = MyManagerQuerySet.as_manager() |
| 215 |
field4 = models.CharField(max_length=30) |
| 216 |
|
| 217 |
|
| 218 |
class ModelArticle(PolymorphicModel): |
| 219 |
sales_points = models.IntegerField() |
| 220 |
|
| 221 |
|
| 222 |
class ModelPackage(ModelArticle): |
| 223 |
name = models.CharField(max_length=300) |
| 224 |
|
| 225 |
|
| 226 |
class ModelComponent(ModelArticle): |
| 227 |
name = models.CharField(max_length=300) |
| 228 |
|
| 229 |
|
| 230 |
class ModelOrderLine(models.Model): |
| 231 |
articles = models.ManyToManyField(ModelArticle, related_name="orderline") |
| 232 |
|
| 233 |
|
| 234 |
class MROBase1(ShowFieldType, PolymorphicModel): |
| 235 |
objects = MyManager() |
| 236 |
field1 = models.CharField(max_length=30) # needed as MyManager uses it |
| 237 |
|
| 238 |
|
| 239 |
class MROBase2(MROBase1): |
| 240 |
pass |
| 241 |
# No manager_inheritance_from_future or Meta set. test that polymorphic restores that. |
| 242 |
|
| 243 |
|
| 244 |
class MROBase3(models.Model): |
| 245 |
# make sure 'id' field doesn't clash, detected by Django 1.11 |
| 246 |
base_3_id = models.AutoField(primary_key=True) |
| 247 |
objects = models.Manager() |
| 248 |
|
| 249 |
|
| 250 |
class MRODerived(MROBase2, MROBase3): |
| 251 |
pass |
| 252 |
|
| 253 |
|
| 254 |
class ParentModelWithManager(PolymorphicModel): |
| 255 |
pass |
| 256 |
|
| 257 |
|
| 258 |
class ChildModelWithManager(PolymorphicModel): |
| 259 |
# Also test whether foreign keys receive the manager: |
| 260 |
field1 = models.CharField(max_length=30) # needed as MyManager uses it |
| 261 |
fk = models.ForeignKey( |
| 262 |
ParentModelWithManager, on_delete=models.CASCADE, related_name="childmodel_set" |
| 263 |
) |
| 264 |
objects = MyManager() |
| 265 |
|
| 266 |
|
| 267 |
class PlainMyManagerQuerySet(QuerySet): |
| 268 |
def my_queryset_foo(self): |
| 269 |
# Just a method to prove the existence of the custom queryset. |
| 270 |
return self.all() |
| 271 |
|
| 272 |
|
| 273 |
class PlainMyManager(models.Manager): |
| 274 |
def my_queryset_foo(self): |
| 275 |
return self.get_queryset().my_queryset_foo() |
| 276 |
|
| 277 |
def get_queryset(self): |
| 278 |
return PlainMyManagerQuerySet(self.model, using=self._db) |
| 279 |
|
| 280 |
|
| 281 |
class PlainParentModelWithManager(models.Model): |
| 282 |
pass |
| 283 |
|
| 284 |
|
| 285 |
class PlainChildModelWithManager(models.Model): |
| 286 |
fk = models.ForeignKey( |
| 287 |
PlainParentModelWithManager, |
| 288 |
on_delete=models.CASCADE, |
| 289 |
related_name="childmodel_set", |
| 290 |
) |
| 291 |
objects = PlainMyManager() |
| 292 |
|
| 293 |
|
| 294 |
class BlogBase(ShowFieldTypeAndContent, PolymorphicModel): |
| 295 |
name = models.CharField(max_length=30) |
| 296 |
|
| 297 |
|
| 298 |
class BlogA(BlogBase): |
| 299 |
info = models.CharField(max_length=30) |
| 300 |
|
| 301 |
|
| 302 |
class BlogB(BlogBase): |
| 303 |
pass |
| 304 |
|
| 305 |
|
| 306 |
class BlogEntry(ShowFieldTypeAndContent, PolymorphicModel): |
| 307 |
blog = models.ForeignKey(BlogA, on_delete=models.CASCADE) |
| 308 |
text = models.CharField(max_length=30) |
| 309 |
|
| 310 |
|
| 311 |
class BlogEntry_limit_choices_to(ShowFieldTypeAndContent, PolymorphicModel): |
| 312 |
blog = models.ForeignKey(BlogBase, on_delete=models.CASCADE) |
| 313 |
text = models.CharField(max_length=30) |
| 314 |
|
| 315 |
|
| 316 |
class ModelFieldNameTest(ShowFieldType, PolymorphicModel): |
| 317 |
modelfieldnametest = models.CharField(max_length=30) |
| 318 |
|
| 319 |
|
| 320 |
class InitTestModel(ShowFieldType, PolymorphicModel): |
| 321 |
bar = models.CharField(max_length=300) |
| 322 |
|
| 323 |
def __init__(self, *args, **kwargs): |
| 324 |
kwargs["bar"] = self.x() |
| 325 |
super().__init__(*args, **kwargs) |
| 326 |
|
| 327 |
|
| 328 |
class InitTestModelSubclass(InitTestModel): |
| 329 |
def x(self): |
| 330 |
return "XYZ" |
| 331 |
|
| 332 |
|
| 333 |
# models from github issue |
| 334 |
|
| 335 |
|
| 336 |
class Top(PolymorphicModel): |
| 337 |
name = models.CharField(max_length=50) |
| 338 |
|
| 339 |
|
| 340 |
class Middle(Top): |
| 341 |
description = models.TextField() |
| 342 |
|
| 343 |
|
| 344 |
class Bottom(Middle): |
| 345 |
author = models.CharField(max_length=50) |
| 346 |
|
| 347 |
|
| 348 |
class UUIDProject(ShowFieldTypeAndContent, PolymorphicModel): |
| 349 |
uuid_primary_key = models.UUIDField(primary_key=True, default=uuid.uuid1) |
| 350 |
topic = models.CharField(max_length=30) |
| 351 |
|
| 352 |
|
| 353 |
class UUIDArtProject(UUIDProject): |
| 354 |
artist = models.CharField(max_length=30) |
| 355 |
|
| 356 |
|
| 357 |
class UUIDResearchProject(UUIDProject): |
| 358 |
supervisor = models.CharField(max_length=30) |
| 359 |
|
| 360 |
|
| 361 |
class UUIDArtProjectA(UUIDArtProject): ... |
| 362 |
|
| 363 |
|
| 364 |
class UUIDArtProjectB(UUIDArtProjectA): ... |
| 365 |
|
| 366 |
|
| 367 |
class UUIDArtProjectC(UUIDArtProjectB): ... |
| 368 |
|
| 369 |
|
| 370 |
class UUIDArtProjectD(UUIDArtProjectC): ... |
| 371 |
|
| 372 |
|
| 373 |
class UUIDPlainA(models.Model): |
| 374 |
uuid_primary_key = models.UUIDField(primary_key=True, default=uuid.uuid1) |
| 375 |
field1 = models.CharField(max_length=30) |
| 376 |
|
| 377 |
|
| 378 |
class UUIDPlainB(UUIDPlainA): |
| 379 |
field2 = models.CharField(max_length=30) |
| 380 |
|
| 381 |
|
| 382 |
class UUIDPlainC(UUIDPlainB): |
| 383 |
field3 = models.CharField(max_length=30) |
| 384 |
|
| 385 |
|
| 386 |
# base -> proxy |
| 387 |
|
| 388 |
|
| 389 |
class ProxyBase(PolymorphicModel): |
| 390 |
some_data = models.CharField(max_length=128) |
| 391 |
|
| 392 |
|
| 393 |
class ProxyChild(ProxyBase): |
| 394 |
class Meta: |
| 395 |
proxy = True |
| 396 |
|
| 397 |
|
| 398 |
class NonProxyChild(ProxyBase): |
| 399 |
name = models.CharField(max_length=30) |
| 400 |
|
| 401 |
|
| 402 |
# base -> proxy -> real models |
| 403 |
|
| 404 |
|
| 405 |
class ProxiedBase(ShowFieldTypeAndContent, PolymorphicModel): |
| 406 |
name = models.CharField(max_length=30) |
| 407 |
|
| 408 |
|
| 409 |
class ProxyModelBase(ProxiedBase): |
| 410 |
class Meta: |
| 411 |
proxy = True |
| 412 |
|
| 413 |
|
| 414 |
class ProxyModelA(ProxyModelBase): |
| 415 |
field1 = models.CharField(max_length=30) |
| 416 |
|
| 417 |
|
| 418 |
class ProxyModelB(ProxyModelBase): |
| 419 |
field2 = models.CharField(max_length=30) |
| 420 |
|
| 421 |
|
| 422 |
# test bad field name |
| 423 |
# class TestBadFieldModel(ShowFieldType, PolymorphicModel): |
| 424 |
# instance_of = models.CharField(max_length=30) |
| 425 |
|
| 426 |
|
| 427 |
# validation error: "polymorphic.relatednameclash: Accessor for field 'polymorphic_ctype' clashes |
| 428 |
# with related field 'ContentType.relatednameclash_set'." (reported by Andrew Ingram) |
| 429 |
# fixed with related_name |
| 430 |
class RelatedNameClash(ShowFieldType, PolymorphicModel): |
| 431 |
ctype = models.ForeignKey(ContentType, on_delete=models.CASCADE, null=True, editable=False) |
| 432 |
|
| 433 |
|
| 434 |
# class with a parent_link to superclass, and a related_name back to subclass |
| 435 |
|
| 436 |
|
| 437 |
class ParentLinkAndRelatedName(ModelShow1_plain): |
| 438 |
superclass = models.OneToOneField( |
| 439 |
ModelShow1_plain, |
| 440 |
on_delete=models.CASCADE, |
| 441 |
parent_link=True, |
| 442 |
related_name="related_name_subclass", |
| 443 |
) |
| 444 |
|
| 445 |
|
| 446 |
class CustomPkBase(ShowFieldTypeAndContent, PolymorphicModel): |
| 447 |
b = models.CharField(max_length=1) |
| 448 |
|
| 449 |
|
| 450 |
class CustomPkInherit(CustomPkBase): |
| 451 |
custom_id = models.AutoField(primary_key=True) |
| 452 |
i = models.CharField(max_length=1) |
| 453 |
|
| 454 |
|
| 455 |
class DateModel(PolymorphicModel): |
| 456 |
date = models.DateTimeField() |
| 457 |
|
| 458 |
|
| 459 |
# Define abstract and swappable (being swapped for SwappedModel) models |
| 460 |
# To test manager validation (should be skipped for such models) |
| 461 |
class AbstractModel(PolymorphicModel): |
| 462 |
class Meta: |
| 463 |
abstract = True |
| 464 |
|
| 465 |
|
| 466 |
class SwappableModel(AbstractModel): |
| 467 |
class Meta: |
| 468 |
swappable = "POLYMORPHIC_TEST_SWAPPABLE" |
| 469 |
|
| 470 |
|
| 471 |
class SwappedModel(AbstractModel): |
| 472 |
pass |
| 473 |
|
| 474 |
|
| 475 |
class InlineParent(models.Model): |
| 476 |
title = models.CharField(max_length=30) |
| 477 |
|
| 478 |
|
| 479 |
class InlineModelA(PolymorphicModel): |
| 480 |
parent = models.ForeignKey( |
| 481 |
InlineParent, related_name="inline_children", on_delete=models.CASCADE |
| 482 |
) |
| 483 |
field1 = models.CharField(max_length=30) |
| 484 |
|
| 485 |
|
| 486 |
class InlineModelB(InlineModelA): |
| 487 |
field2 = models.CharField(max_length=30) |
| 488 |
|
| 489 |
plain_a = models.ForeignKey( |
| 490 |
PlainA, |
| 491 |
null=True, |
| 492 |
blank=True, |
| 493 |
default=None, |
| 494 |
on_delete=models.SET_NULL, |
| 495 |
related_name="inline_bs", |
| 496 |
) |
| 497 |
|
| 498 |
# File field for testing multipart encoding in polymorphic inlines (issue #380) |
| 499 |
file_upload = models.FileField(upload_to="test_uploads/", null=True, blank=True, default=None) |
| 500 |
|
| 501 |
|
| 502 |
class AbstractProject(PolymorphicModel): |
| 503 |
topic = models.CharField(max_length=30) |
| 504 |
|
| 505 |
class Meta: |
| 506 |
abstract = True |
| 507 |
|
| 508 |
|
| 509 |
class ArtProject(AbstractProject): |
| 510 |
artist = models.CharField(max_length=30) |
| 511 |
|
| 512 |
|
| 513 |
class Duck(PolymorphicModel): |
| 514 |
name = models.CharField(max_length=30) |
| 515 |
|
| 516 |
|
| 517 |
class RedheadDuck(Duck): |
| 518 |
class Meta: |
| 519 |
proxy = True |
| 520 |
|
| 521 |
|
| 522 |
class RubberDuck(Duck): |
| 523 |
class Meta: |
| 524 |
proxy = True |
| 525 |
|
| 526 |
|
| 527 |
class MultiTableBase(PolymorphicModel): |
| 528 |
field1 = models.CharField(max_length=30) |
| 529 |
|
| 530 |
|
| 531 |
class MultiTableDerived(MultiTableBase): |
| 532 |
field2 = models.CharField(max_length=30) |
| 533 |
|
| 534 |
|
| 535 |
class SubclassSelectorAbstractBaseModel(PolymorphicModel): |
| 536 |
base_field = models.CharField(max_length=30, default="test_bf") |
| 537 |
|
| 538 |
|
| 539 |
class SubclassSelectorAbstractModel(SubclassSelectorAbstractBaseModel): |
| 540 |
abstract_field = models.CharField(max_length=30, default="test_af") |
| 541 |
|
| 542 |
class Meta: |
| 543 |
abstract = True |
| 544 |
|
| 545 |
|
| 546 |
class SubclassSelectorAbstractConcreteModel(SubclassSelectorAbstractModel): |
| 547 |
concrete_field = models.CharField(max_length=30, default="test_cf") |
| 548 |
|
| 549 |
|
| 550 |
class SubclassSelectorProxyBaseModel(PolymorphicModel): |
| 551 |
base_field = models.CharField(max_length=30, default="test_bf") |
| 552 |
|
| 553 |
|
| 554 |
class SubclassSelectorProxyModel(SubclassSelectorProxyBaseModel): |
| 555 |
class Meta: |
| 556 |
proxy = True |
| 557 |
|
| 558 |
|
| 559 |
class SubclassSelectorProxyConcreteModel(SubclassSelectorProxyModel): |
| 560 |
concrete_field = models.CharField(max_length=30, default="test_cf") |
| 561 |
|
| 562 |
|
| 563 |
class NonPolymorphicParent(PolymorphicModel, Group): |
| 564 |
test = models.CharField(max_length=30, default="test_non_polymorphic_parent") |
| 565 |
|
| 566 |
|
| 567 |
class Participant(PolymorphicModel): |
| 568 |
pass |
| 569 |
|
| 570 |
|
| 571 |
class UserProfile(Participant): |
| 572 |
name = models.CharField(max_length=100) |
| 573 |
|
| 574 |
def __str__(self): |
| 575 |
return self.name |
| 576 |
|
| 577 |
|
| 578 |
class Team(models.Model): |
| 579 |
team_name = models.CharField(max_length=100) |
| 580 |
user_profiles = models.ManyToManyField(UserProfile, related_name="user_teams") |
| 581 |
|
| 582 |
|
| 583 |
class BlueHeadDuck(Duck): |
| 584 |
def __init__(self, *args, **kwargs): |
| 585 |
super().__init__(*args, **kwargs) |
| 586 |
self.color = "blue" |
| 587 |
|
| 588 |
|
| 589 |
class HomeDuck(models.Model): |
| 590 |
def __init__(self, *args, **kwargs): |
| 591 |
super().__init__(*args, **kwargs) |
| 592 |
self.home = "Duckburg" |
| 593 |
|
| 594 |
class Meta: |
| 595 |
abstract = True |
| 596 |
|
| 597 |
|
| 598 |
class PurpleHeadDuck(HomeDuck, BlueHeadDuck): |
| 599 |
class Meta: |
| 600 |
proxy = True |
| 601 |
|
| 602 |
|
| 603 |
class Account(PolymorphicModel): |
| 604 |
user = models.OneToOneField( |
| 605 |
get_user_model(), primary_key=True, on_delete=models.CASCADE, related_name="account" |
| 606 |
) |
| 607 |
|
| 608 |
|
| 609 |
class SpecialAccount1(Account): |
| 610 |
extra1 = models.IntegerField(null=True, default=None, blank=True) |
| 611 |
|
| 612 |
|
| 613 |
class SpecialAccount1_1(SpecialAccount1): |
| 614 |
extra2 = models.IntegerField(null=True, default=None, blank=True) |
| 615 |
|
| 616 |
|
| 617 |
class SpecialAccount2(Account): |
| 618 |
extra1 = models.CharField(default="", blank=True, max_length=30) |
| 619 |
|
| 620 |
|
| 621 |
class ModelMixin(models.Model): |
| 622 |
class Meta: |
| 623 |
abstract = True |
| 624 |
|
| 625 |
created_at = models.DateTimeField(auto_now_add=True) |
| 626 |
modified_at = models.DateTimeField(auto_now=True) |
| 627 |
|
| 628 |
|
| 629 |
class PolymorphicMixin(PolymorphicModel): |
| 630 |
class Meta: |
| 631 |
abstract = True |
| 632 |
|
| 633 |
created_at = models.DateTimeField(auto_now_add=True) |
| 634 |
modified_at = models.DateTimeField(auto_now=True) |
| 635 |
|
| 636 |
|
| 637 |
class Foo(PolymorphicModel): |
| 638 |
pass |
| 639 |
|
| 640 |
|
| 641 |
class Bar(PolymorphicMixin, PolymorphicModel): |
| 642 |
pass |
| 643 |
|
| 644 |
|
| 645 |
class Baz(ModelMixin, PolymorphicModel): |
| 646 |
pass |
| 647 |
|
| 648 |
|
| 649 |
class MyBaseQuerySet(PolymorphicQuerySet): |
| 650 |
def filter_by_user(self, _): |
| 651 |
return self.all() |
| 652 |
|
| 653 |
|
| 654 |
class MyBaseModel(PolymorphicModel): |
| 655 |
... |
| 656 |
objects = MyBaseQuerySet.as_manager() |
| 657 |
|
| 658 |
|
| 659 |
class MyChild1QuerySet(MyBaseQuerySet): |
| 660 |
def filter_by_user(self, num): |
| 661 |
return self.filter(fieldA__lt=num) |
| 662 |
|
| 663 |
|
| 664 |
class MyChild1Model(MyBaseModel): |
| 665 |
fieldA = models.IntegerField() |
| 666 |
... |
| 667 |
objects = MyChild1QuerySet.as_manager() |
| 668 |
|
| 669 |
|
| 670 |
class MyChild2QuerySet(MyBaseQuerySet): |
| 671 |
def filter_by_user(self, num): |
| 672 |
return self.filter(fieldB__gt=num) |
| 673 |
|
| 674 |
|
| 675 |
class MyChild2Model(MyBaseModel): |
| 676 |
fieldB = models.IntegerField() |
| 677 |
... |
| 678 |
objects = PolymorphicManager.from_queryset(MyChild2QuerySet)() |
| 679 |
base_manager = MyBaseQuerySet.as_manager() |
| 680 |
|
| 681 |
|
| 682 |
class SpecialQuerySet(PolymorphicQuerySet): |
| 683 |
def has_text(self, text): |
| 684 |
return self.filter(abstract_field__icontains=text) |
| 685 |
|
| 686 |
|
| 687 |
class SpecialPolymorphicManager(PolymorphicManager.from_queryset(SpecialQuerySet)): |
| 688 |
def custom_queryset(self): |
| 689 |
return self.get_queryset() |
| 690 |
|
| 691 |
|
| 692 |
class AbstractManagerTest(PolymorphicModel): |
| 693 |
""" |
| 694 |
Tests that custom manager patterns work on abstract base models |
| 695 |
""" |
| 696 |
|
| 697 |
objects = SpecialPolymorphicManager() |
| 698 |
basic_manager = Manager() |
| 699 |
default_manager = PolymorphicManager() |
| 700 |
|
| 701 |
abstract_field = models.CharField(max_length=32) |
| 702 |
|
| 703 |
class Meta: |
| 704 |
abstract = True |
| 705 |
|
| 706 |
|
| 707 |
class RelatedManagerTest(models.Model): ... |
| 708 |
|
| 709 |
|
| 710 |
class DerivedManagerTest(AbstractManagerTest): |
| 711 |
related_test = models.ForeignKey( |
| 712 |
RelatedManagerTest, |
| 713 |
on_delete=models.CASCADE, |
| 714 |
default=None, |
| 715 |
null=True, |
| 716 |
related_name="derived", |
| 717 |
) |
| 718 |
|
| 719 |
|
| 720 |
class DerivedManagerTest2(DerivedManagerTest): |
| 721 |
objects = PolymorphicManager() |
| 722 |
|
| 723 |
|
| 724 |
class FKTestBase(PolymorphicModel): ... |
| 725 |
|
| 726 |
|
| 727 |
class FKTestChild(Base): ... |
| 728 |
|
| 729 |
|
| 730 |
class FKTest(models.Model): |
| 731 |
fk = models.ForeignKey(Base, null=True, on_delete=models.SET_NULL) |
| 732 |
|
| 733 |
|
| 734 |
class NoChildren(PolymorphicModel): |
| 735 |
field1 = models.CharField(max_length=12) |
| 736 |
|
| 737 |
|
| 738 |
class ModelWithPolyFK(models.Model): |
| 739 |
"""Model with FK to polymorphic model for popup testing.""" |
| 740 |
|
| 741 |
name = models.CharField(max_length=100) |
| 742 |
poly_fk = models.ForeignKey(Model2A, on_delete=models.CASCADE, null=True, blank=True) |
| 743 |
|
| 744 |
|
| 745 |
class NormalBase(models.Model): |
| 746 |
nb_field = models.IntegerField() |
| 747 |
|
| 748 |
def add_to_nb(self, value): |
| 749 |
self.nb_field += value |
| 750 |
self.save(update_fields=["nb_field"]) |
| 751 |
|
| 752 |
|
| 753 |
class NormalExtension(NormalBase): |
| 754 |
ne_field = models.CharField(max_length=50) |
| 755 |
|
| 756 |
def add_to_ne(self, value): |
| 757 |
self.ne_field += value |
| 758 |
self.save(update_fields=["ne_field"]) |
| 759 |
|
| 760 |
|
| 761 |
class PolyExtension(PolymorphicModel, NormalExtension): |
| 762 |
poly_ext_field = models.IntegerField() |
| 763 |
|
| 764 |
def add_to_ext(self, value): |
| 765 |
self.poly_ext_field += value |
| 766 |
self.save(update_fields=["poly_ext_field"]) |
| 767 |
|
| 768 |
|
| 769 |
class PolyExtChild(PolyExtension): |
| 770 |
poly_child_field = models.CharField(max_length=50) |
| 771 |
|
| 772 |
def add_to_child(self, value): |
| 773 |
self.poly_child_field += value |
| 774 |
self.save(update_fields=["poly_child_field"]) |
| 775 |
|
| 776 |
def override_add_to_ne(self, value): |
| 777 |
# test that we can still access NormalExtension methods |
| 778 |
self.ne_field += value.upper() |
| 779 |
self.save(update_fields=["ne_field"]) |
| 780 |
|
| 781 |
def override_add_to_ext(self, value): |
| 782 |
# test that we can still access PolyExtension methods |
| 783 |
self.poly_ext_field += value * 2 |
| 784 |
self.save(update_fields=["poly_ext_field"]) |
| 785 |
|
| 786 |
|
| 787 |
class DeepCopyTester(PolymorphicModel): |
| 788 |
binary_field = models.BinaryField() |
| 789 |
|
| 790 |
|
| 791 |
class DeepCopyTester2(DeepCopyTester): |
| 792 |
binary_field2 = models.BinaryField() |
| 793 |
|
| 794 |
|
| 795 |
class DucksLake(models.Model): |
| 796 |
lake = models.ForeignKey("LakeWithThrough", on_delete=models.CASCADE) |
| 797 |
duck = models.ForeignKey(Duck, on_delete=models.CASCADE) |
| 798 |
time = models.CharField(max_length=10) |
| 799 |
|
| 800 |
|
| 801 |
class Lake(models.Model): |
| 802 |
ducks = models.ManyToManyField(Duck) |
| 803 |
|
| 804 |
|
| 805 |
class LakeWithThrough(models.Model): |
| 806 |
ducks = models.ManyToManyField(Duck, through=DucksLake) |
| 807 |
|
| 808 |
|
| 809 |
class ChoiceBlank(PolymorphicModel): |
| 810 |
pass |
| 811 |
|
| 812 |
|
| 813 |
class ChoiceAthlete(ChoiceBlank): |
| 814 |
choice = models.CharField(max_length=100) |
| 815 |
|
| 816 |
|
| 817 |
class BetMultiple(models.Model): |
| 818 |
answer = models.ManyToManyField("ChoiceBlank", blank=True, through="RankedAthlete") |
| 819 |
|
| 820 |
|
| 821 |
class RankedAthlete(models.Model): |
| 822 |
choiceAthlete = models.ForeignKey("ChoiceBlank", on_delete=models.CASCADE) |
| 823 |
bet = models.ForeignKey("BetMultiple", on_delete=models.CASCADE) |
| 824 |
rank = models.IntegerField() |
| 825 |
|
| 826 |
|
| 827 |
class RecursionBug(PolymorphicModel): |
| 828 |
status = models.ForeignKey(PlainA, on_delete=models.CASCADE, related_name="recursions") |
| 829 |
|
| 830 |
def __init__(self, *args, **kwargs): |
| 831 |
""" |
| 832 |
https://github.com/jazzband/django-polymorphic/issues/334 |
| 833 |
""" |
| 834 |
super().__init__(*args, **kwargs) |
| 835 |
self.old_status_id = self.status_id |
| 836 |
|
| 837 |
|
| 838 |
class TaggedItem(models.Model): |
| 839 |
tag = models.SlugField() |
| 840 |
content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE) |
| 841 |
object_id = models.PositiveIntegerField() |
| 842 |
content_object = GenericForeignKey("content_type", "object_id") |
| 843 |
|
| 844 |
|
| 845 |
class BookmarkManager(PolymorphicManager): |
| 846 |
def get_queryset(self) -> PolymorphicQuerySet: |
| 847 |
return super().get_queryset().annotate(cpy=models.F("url")) |
| 848 |
|
| 849 |
|
| 850 |
class Bookmark(PolymorphicModel): |
| 851 |
url = models.URLField() |
| 852 |
tags = GenericRelation(TaggedItem) |
| 853 |
objects = BookmarkManager() |
| 854 |
|
| 855 |
|
| 856 |
class Assignment(Bookmark): |
| 857 |
assigned_to = models.CharField(max_length=100) |
| 858 |
|
| 859 |
|
| 860 |
class Regression295Related(models.Model): |
| 861 |
_real_field = models.CharField(max_length=10) |
| 862 |
|
| 863 |
|
| 864 |
class Regression295Parent(PolymorphicModel): |
| 865 |
related_object = models.ForeignKey(Regression295Related, on_delete=models.CASCADE) |
| 866 |
|
| 867 |
|
| 868 |
class RelatedKeyModel(models.Model): |
| 869 |
custom_id = models.UUIDField(primary_key=True, default=uuid.uuid4) |
| 870 |
|
| 871 |
|
| 872 |
class DisparateKeysParent(PolymorphicModel): |
| 873 |
text = models.CharField(max_length=30) |
| 874 |
|
| 875 |
|
| 876 |
class DisparateKeysChild1(DisparateKeysParent): |
| 877 |
key = models.OneToOneField(RelatedKeyModel, primary_key=True, on_delete=models.CASCADE) |
| 878 |
|
| 879 |
text_child1 = models.CharField(max_length=30) |
| 880 |
|
| 881 |
|
| 882 |
class DisparateKeysChild2(DisparateKeysParent): |
| 883 |
text_child2 = models.CharField(max_length=30) |
| 884 |
key = models.PositiveIntegerField(primary_key=True) |
| 885 |
|
| 886 |
|
| 887 |
class DisparateKeysGrandChild2(DisparateKeysChild2): |
| 888 |
text_grand_child = models.CharField(max_length=30) |
| 889 |
|
| 890 |
|
| 891 |
class DisparateKeysGrandChild(DisparateKeysChild1): |
| 892 |
text_grand_child = models.CharField(max_length=30) |
| 893 |
|
| 894 |
|
| 895 |
class M2MAdminTest(PolymorphicModel): |
| 896 |
name = models.CharField(max_length=30) |
| 897 |
|
| 898 |
def __str__(self): |
| 899 |
return self.name |
| 900 |
|
| 901 |
|
| 902 |
class M2MAdminTestChildA(M2MAdminTest): |
| 903 |
child_bs = models.ManyToManyField("M2MAdminTestChildB", related_name="related_as", blank=True) |
| 904 |
|
| 905 |
|
| 906 |
class M2MAdminTestChildB(M2MAdminTest): |
| 907 |
child_as = models.ManyToManyField("M2MAdminTestChildA", related_name="related_bs", blank=True) |
| 908 |
|
| 909 |
|
| 910 |
class M2MAdminTestChildC(M2MAdminTestChildB): |
| 911 |
pass |
| 912 |
|
| 913 |
|
| 914 |
# Models for testing Issue #182 and #375: M2M with through tables to/from polymorphic models |
| 915 |
class M2MThroughBase(PolymorphicModel): |
| 916 |
"""Base polymorphic model for M2M through table tests.""" |
| 917 |
|
| 918 |
name = models.CharField(max_length=50) |
| 919 |
|
| 920 |
def __str__(self): |
| 921 |
return self.name |
| 922 |
|
| 923 |
|
| 924 |
class M2MThroughPerson(M2MThroughBase): |
| 925 |
"""Polymorphic child representing a person who can be on teams.""" |
| 926 |
|
| 927 |
email = models.EmailField(blank=True) |
| 928 |
|
| 929 |
|
| 930 |
class M2MThroughSpecialPerson(M2MThroughPerson): |
| 931 |
"""Polymorphic child representing a special person.""" |
| 932 |
|
| 933 |
special_code = models.CharField(max_length=20, blank=True) |
| 934 |
|
| 935 |
|
| 936 |
class M2MThroughProject(M2MThroughBase): |
| 937 |
"""Polymorphic child representing a project.""" |
| 938 |
|
| 939 |
description = models.TextField(blank=True) |
| 940 |
|
| 941 |
|
| 942 |
class M2MThroughProjectWithTeam(M2MThroughProject): |
| 943 |
""" |
| 944 |
Polymorphic child with M2M to Person through Membership. |
| 945 |
Tests Issue #375: M2M with through table on polymorphic model. |
| 946 |
""" |
| 947 |
|
| 948 |
pass |
| 949 |
|
| 950 |
|
| 951 |
class M2MThroughMembership(PolymorphicModel): |
| 952 |
"""Polymorphic through model for M2M relationship between ProjectWithTeam and Person.""" |
| 953 |
|
| 954 |
project = models.ForeignKey("M2MThroughProjectWithTeam", on_delete=models.CASCADE) |
| 955 |
person = models.ForeignKey(M2MThroughPerson, on_delete=models.CASCADE) |
| 956 |
role = models.CharField(max_length=50) |
| 957 |
joined_date = models.DateField(auto_now_add=True) |
| 958 |
|
| 959 |
def __str__(self): |
| 960 |
return f"{self.person.name} - {self.role} on {self.project.name}" |
| 961 |
|
| 962 |
|
| 963 |
class M2MThroughMembershipWithPerson(M2MThroughMembership): |
| 964 |
"""Membership for regular Person instances.""" |
| 965 |
|
| 966 |
pass |
| 967 |
|
| 968 |
|
| 969 |
class M2MThroughMembershipWithSpecialPerson(M2MThroughMembership): |
| 970 |
"""Membership for SpecialPerson instances with additional tracking.""" |
| 971 |
|
| 972 |
special_notes = models.TextField(blank=True, default="") |
| 973 |
|
| 974 |
|
| 975 |
# Add the M2M field after the through model is defined |
| 976 |
M2MThroughProjectWithTeam.add_to_class( |
| 977 |
"team", |
| 978 |
models.ManyToManyField( |
| 979 |
M2MThroughPerson, through=M2MThroughMembership, related_name="projects", blank=True |
| 980 |
), |
| 981 |
) |
| 982 |
|
| 983 |
|
| 984 |
# Additional models for Issue #182: Direct M2M to polymorphic model |
| 985 |
class DirectM2MContainer(models.Model): |
| 986 |
"""Non-polymorphic model with direct M2M to polymorphic model.""" |
| 987 |
|
| 988 |
name = models.CharField(max_length=50) |
| 989 |
items = models.ManyToManyField(M2MThroughBase, related_name="containers", blank=True) |
| 990 |
|
| 991 |
def __str__(self): |
| 992 |
return self.name |
| 993 |
|
| 994 |
|
| 995 |
class Author(models.Model): |
| 996 |
pass |
| 997 |
|
| 998 |
|
| 999 |
class Book(PolymorphicModel): |
| 1000 |
author = models.ForeignKey(Author, on_delete=models.CASCADE) |
| 1001 |
|
| 1002 |
|
| 1003 |
class SpecialBook(Book): |
| 1004 |
pass |
| 1005 |
|
| 1006 |
|
| 1007 |
class DeferredManager(PolymorphicManager): |
| 1008 |
def get_queryset(self): |
| 1009 |
return super().get_queryset().defer("field2") |
| 1010 |
|
| 1011 |
|
| 1012 |
class FilteredManager(PolymorphicManager): |
| 1013 |
def get_queryset(self): |
| 1014 |
return super().get_queryset().exclude(field2=Upper(F("field2"))) |
| 1015 |
|
| 1016 |
|
| 1017 |
class Model2BFiltered(Model2B): |
| 1018 |
objects = FilteredManager() |
| 1019 |
|
| 1020 |
|
| 1021 |
class Model2CFiltered(Model2BFiltered): |
| 1022 |
field3 = models.CharField(max_length=30, blank=True, default="") |
| 1023 |
|
| 1024 |
|
| 1025 |
class CustomBaseManager(PolymorphicManager): |
| 1026 |
pass |
| 1027 |
|
| 1028 |
|
| 1029 |
class FilteredManager2(FilteredManager): |
| 1030 |
pass |
| 1031 |
|
| 1032 |
|
| 1033 |
class Model2BDeferredManager(Model2B): |
| 1034 |
objects = DeferredManager() |
| 1035 |
|
| 1036 |
class Meta: |
| 1037 |
base_manager_name = "objects" |
| 1038 |
|
| 1039 |
|
| 1040 |
class Model2CNamedManagers(Model2CFiltered): |
| 1041 |
all_objects = CustomBaseManager() |
| 1042 |
filtered_objects = FilteredManager2() |
| 1043 |
|
| 1044 |
class Meta: |
| 1045 |
base_manager_name = "all_objects" |
| 1046 |
default_manager_name = "filtered_objects" |
| 1047 |
|
| 1048 |
|
| 1049 |
class Model2CNamedDefault(Model2CFiltered): |
| 1050 |
custom_objects = FilteredManager2() |
| 1051 |
|
| 1052 |
class Meta: |
| 1053 |
default_manager_name = "custom_objects" |
| 1054 |
|
| 1055 |
|
| 1056 |
# serialization natural key tests #517 |
| 1057 |
class NatKeyManager(PolymorphicManager): |
| 1058 |
def get_by_natural_key(self, slug): |
| 1059 |
return self.get(slug=slug) |
| 1060 |
|
| 1061 |
|
| 1062 |
class NatKeyParent(PolymorphicModel): |
| 1063 |
slug = models.SlugField(unique=True) |
| 1064 |
content = models.CharField(blank=True, max_length=100) |
| 1065 |
|
| 1066 |
objects = NatKeyManager() |
| 1067 |
|
| 1068 |
def natural_key(self): |
| 1069 |
return (self.slug,) |
| 1070 |
|
| 1071 |
|
| 1072 |
class NatKeyChild(NatKeyParent): |
| 1073 |
foo = models.OneToOneField(NatKeyParent, models.CASCADE, parent_link=True, primary_key=True) |
| 1074 |
val = models.IntegerField(default=0) |
| 1075 |
|
| 1076 |
def natural_key(self): |
| 1077 |
return self.foo.natural_key() |
| 1078 |
|
| 1079 |
natural_key.dependencies = ["tests.natkeyparent"] |
| 1080 |
|
| 1081 |
|
| 1082 |
class ManagerTest(PolymorphicModel): |
| 1083 |
name = models.CharField(max_length=30) |
| 1084 |
|
| 1085 |
objects = CustomBaseManager() |
| 1086 |
|
| 1087 |
class Meta: |
| 1088 |
base_manager_name = "objects" |
| 1089 |
|
| 1090 |
|
| 1091 |
class ManagerTestChild(ManagerTest): |
| 1092 |
pass |
| 1093 |
|
| 1094 |
|
| 1095 |
class PlainManager(models.Manager): ... |
| 1096 |
|
| 1097 |
|
| 1098 |
class ManagerTestPlain(models.Model): |
| 1099 |
objects = PlainManager() |
| 1100 |
|
| 1101 |
class Meta: |
| 1102 |
base_manager_name = "objects" |
| 1103 |
|
| 1104 |
|
| 1105 |
class ManagerTestChildPlain(ManagerTestPlain): |
| 1106 |
pass |
| 1107 |
|
| 1108 |
|
| 1109 |
# Models for testing generic polymorphic formsets |
| 1110 |
class GenericFKParent(models.Model): |
| 1111 |
"""A parent model that generic polymorphic items can point to.""" |
| 1112 |
|
| 1113 |
name = models.CharField(max_length=100) |
| 1114 |
|
| 1115 |
def __str__(self): |
| 1116 |
return self.name |
| 1117 |
|
| 1118 |
|
| 1119 |
class PolymorphicTagBase(PolymorphicModel): |
| 1120 |
"""Base polymorphic model with a GenericForeignKey for testing generic formsets.""" |
| 1121 |
|
| 1122 |
tag = models.SlugField() |
| 1123 |
content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE) |
| 1124 |
object_id = models.PositiveIntegerField() |
| 1125 |
content_object = GenericForeignKey("content_type", "object_id") |
| 1126 |
|
| 1127 |
def __str__(self): |
| 1128 |
return self.tag |
| 1129 |
|
| 1130 |
|
| 1131 |
class PolymorphicTagA(PolymorphicTagBase): |
| 1132 |
"""First child of PolymorphicTagBase with an extra field.""" |
| 1133 |
|
| 1134 |
priority = models.IntegerField(default=0) |
| 1135 |
|
| 1136 |
|
| 1137 |
class PolymorphicTagB(PolymorphicTagBase): |
| 1138 |
"""Second child of PolymorphicTagBase with a different extra field.""" |
| 1139 |
|
| 1140 |
color = models.CharField(max_length=20, default="") |
| 1141 |
|
| 1142 |
|
| 1143 |
class SaveKwargsMixin(models.Model): |
| 1144 |
""" |
| 1145 |
Mimics third-party abstract model mixins (e.g. django-lifecycle) whose |
| 1146 |
save() accepts extra keyword arguments (issue #905). |
| 1147 |
""" |
| 1148 |
|
| 1149 |
class Meta: |
| 1150 |
abstract = True |
| 1151 |
|
| 1152 |
def save(self, *args, custom_kwarg=None, **kwargs): |
| 1153 |
self.captured_custom_kwarg = custom_kwarg |
| 1154 |
super().save(*args, **kwargs) |
| 1155 |
|
| 1156 |
|
| 1157 |
class SaveKwargsModel(PolymorphicModel, SaveKwargsMixin): |
| 1158 |
"""PolymorphicModel.save() must forward extra kwargs down the MRO (issue #905).""" |
| 1159 |
|
| 1160 |
name = models.CharField(max_length=50) |