| 1 |
from django.contrib.contenttypes.fields import GenericForeignKey, GenericRelation |
| 2 |
from django.contrib.contenttypes.models import ContentType |
| 3 |
from django.db import models |
| 4 |
from django.utils.translation import gettext_lazy as _ |
| 5 |
|
| 6 |
|
| 7 |
class Relation(models.Model): |
| 8 |
pass |
| 9 |
|
| 10 |
|
| 11 |
class InstanceOnlyDescriptor: |
| 12 |
def __get__(self, instance, cls=None): |
| 13 |
if instance is None: |
| 14 |
raise AttributeError("Instance only") |
| 15 |
return 1 |
| 16 |
|
| 17 |
|
| 18 |
class AbstractPerson(models.Model): |
| 19 |
# DATA fields |
| 20 |
data_abstract = models.CharField(max_length=10) |
| 21 |
fk_abstract = models.ForeignKey( |
| 22 |
Relation, models.CASCADE, related_name="fk_abstract_rel" |
| 23 |
) |
| 24 |
|
| 25 |
# M2M fields |
| 26 |
m2m_abstract = models.ManyToManyField(Relation, related_name="m2m_abstract_rel") |
| 27 |
friends_abstract = models.ManyToManyField("self", symmetrical=True) |
| 28 |
following_abstract = models.ManyToManyField( |
| 29 |
"self", related_name="followers_abstract", symmetrical=False |
| 30 |
) |
| 31 |
|
| 32 |
# VIRTUAL fields |
| 33 |
data_not_concrete_abstract = models.ForeignObject( |
| 34 |
Relation, |
| 35 |
on_delete=models.CASCADE, |
| 36 |
from_fields=["abstract_non_concrete_id"], |
| 37 |
to_fields=["id"], |
| 38 |
related_name="fo_abstract_rel", |
| 39 |
) |
| 40 |
|
| 41 |
# GFK fields |
| 42 |
content_type_abstract = models.ForeignKey( |
| 43 |
ContentType, models.CASCADE, related_name="+" |
| 44 |
) |
| 45 |
object_id_abstract = models.PositiveIntegerField() |
| 46 |
content_object_abstract = GenericForeignKey( |
| 47 |
"content_type_abstract", "object_id_abstract" |
| 48 |
) |
| 49 |
|
| 50 |
# GR fields |
| 51 |
generic_relation_abstract = GenericRelation(Relation) |
| 52 |
|
| 53 |
class Meta: |
| 54 |
abstract = True |
| 55 |
|
| 56 |
@property |
| 57 |
def test_property(self): |
| 58 |
return 1 |
| 59 |
|
| 60 |
test_instance_only_descriptor = InstanceOnlyDescriptor() |
| 61 |
|
| 62 |
|
| 63 |
class BasePerson(AbstractPerson): |
| 64 |
# DATA fields |
| 65 |
data_base = models.CharField(max_length=10) |
| 66 |
fk_base = models.ForeignKey(Relation, models.CASCADE, related_name="fk_base_rel") |
| 67 |
|
| 68 |
# M2M fields |
| 69 |
m2m_base = models.ManyToManyField(Relation, related_name="m2m_base_rel") |
| 70 |
friends_base = models.ManyToManyField("self", symmetrical=True) |
| 71 |
following_base = models.ManyToManyField( |
| 72 |
"self", related_name="followers_base", symmetrical=False |
| 73 |
) |
| 74 |
|
| 75 |
# VIRTUAL fields |
| 76 |
data_not_concrete_base = models.ForeignObject( |
| 77 |
Relation, |
| 78 |
on_delete=models.CASCADE, |
| 79 |
from_fields=["base_non_concrete_id"], |
| 80 |
to_fields=["id"], |
| 81 |
related_name="fo_base_rel", |
| 82 |
) |
| 83 |
|
| 84 |
# GFK fields |
| 85 |
content_type_base = models.ForeignKey(ContentType, models.CASCADE, related_name="+") |
| 86 |
object_id_base = models.PositiveIntegerField() |
| 87 |
content_object_base = GenericForeignKey("content_type_base", "object_id_base") |
| 88 |
|
| 89 |
# GR fields |
| 90 |
generic_relation_base = GenericRelation(Relation) |
| 91 |
|
| 92 |
|
| 93 |
class Person(BasePerson): |
| 94 |
# DATA fields |
| 95 |
data_inherited = models.CharField(max_length=10) |
| 96 |
fk_inherited = models.ForeignKey( |
| 97 |
Relation, models.CASCADE, related_name="fk_concrete_rel" |
| 98 |
) |
| 99 |
|
| 100 |
# M2M Fields |
| 101 |
m2m_inherited = models.ManyToManyField(Relation, related_name="m2m_concrete_rel") |
| 102 |
friends_inherited = models.ManyToManyField("self", symmetrical=True) |
| 103 |
following_inherited = models.ManyToManyField( |
| 104 |
"self", related_name="followers_concrete", symmetrical=False |
| 105 |
) |
| 106 |
|
| 107 |
# VIRTUAL fields |
| 108 |
data_not_concrete_inherited = models.ForeignObject( |
| 109 |
Relation, |
| 110 |
on_delete=models.CASCADE, |
| 111 |
from_fields=["model_non_concrete_id"], |
| 112 |
to_fields=["id"], |
| 113 |
related_name="fo_concrete_rel", |
| 114 |
) |
| 115 |
|
| 116 |
# GFK fields |
| 117 |
content_type_concrete = models.ForeignKey( |
| 118 |
ContentType, models.CASCADE, related_name="+" |
| 119 |
) |
| 120 |
object_id_concrete = models.PositiveIntegerField() |
| 121 |
content_object_concrete = GenericForeignKey( |
| 122 |
"content_type_concrete", "object_id_concrete" |
| 123 |
) |
| 124 |
|
| 125 |
# GR fields |
| 126 |
generic_relation_concrete = GenericRelation(Relation) |
| 127 |
|
| 128 |
class Meta: |
| 129 |
verbose_name = _("Person") |
| 130 |
|
| 131 |
|
| 132 |
class ProxyPerson(Person): |
| 133 |
class Meta: |
| 134 |
proxy = True |
| 135 |
|
| 136 |
|
| 137 |
class PersonThroughProxySubclass(ProxyPerson): |
| 138 |
pass |
| 139 |
|
| 140 |
|
| 141 |
class Relating(models.Model): |
| 142 |
# ForeignKey to BasePerson |
| 143 |
baseperson = models.ForeignKey( |
| 144 |
BasePerson, models.CASCADE, related_name="relating_baseperson" |
| 145 |
) |
| 146 |
baseperson_hidden = models.ForeignKey(BasePerson, models.CASCADE, related_name="+") |
| 147 |
|
| 148 |
# ForeignKey to Person |
| 149 |
person = models.ForeignKey(Person, models.CASCADE, related_name="relating_person") |
| 150 |
person_hidden = models.ForeignKey(Person, models.CASCADE, related_name="+") |
| 151 |
|
| 152 |
# ForeignKey to ProxyPerson |
| 153 |
proxyperson = models.ForeignKey( |
| 154 |
ProxyPerson, models.CASCADE, related_name="relating_proxyperson" |
| 155 |
) |
| 156 |
proxyperson_hidden = models.ForeignKey( |
| 157 |
ProxyPerson, models.CASCADE, related_name="relating_proxyperson_hidden+" |
| 158 |
) |
| 159 |
|
| 160 |
# ManyToManyField to BasePerson |
| 161 |
basepeople = models.ManyToManyField(BasePerson, related_name="relating_basepeople") |
| 162 |
basepeople_hidden = models.ManyToManyField(BasePerson, related_name="+") |
| 163 |
|
| 164 |
# ManyToManyField to Person |
| 165 |
people = models.ManyToManyField(Person, related_name="relating_people") |
| 166 |
people_hidden = models.ManyToManyField(Person, related_name="+") |
| 167 |
|
| 168 |
|
| 169 |
class Swappable(models.Model): |
| 170 |
class Meta: |
| 171 |
swappable = "MODEL_META_TESTS_SWAPPED" |
| 172 |
|
| 173 |
|
| 174 |
# ParentListTests models |
| 175 |
class CommonAncestor(models.Model): |
| 176 |
pass |
| 177 |
|
| 178 |
|
| 179 |
class FirstParent(CommonAncestor): |
| 180 |
first_ancestor = models.OneToOneField( |
| 181 |
CommonAncestor, models.CASCADE, primary_key=True, parent_link=True |
| 182 |
) |
| 183 |
|
| 184 |
|
| 185 |
class SecondParent(CommonAncestor): |
| 186 |
second_ancestor = models.OneToOneField( |
| 187 |
CommonAncestor, models.CASCADE, primary_key=True, parent_link=True |
| 188 |
) |
| 189 |
|
| 190 |
|
| 191 |
class Child(FirstParent, SecondParent): |
| 192 |
pass |