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