2015-01-28 20:35:27 +08:00
|
|
|
from django.contrib.contenttypes.fields import GenericForeignKey, GenericRelation
|
2014-06-19 22:03:36 +08:00
|
|
|
from django.contrib.contenttypes.models import ContentType
|
2015-01-28 20:35:27 +08:00
|
|
|
from django.db import models
|
2014-06-19 22:03:36 +08:00
|
|
|
|
|
|
|
|
|
|
|
class Relation(models.Model):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2017-06-26 22:30:31 +08:00
|
|
|
class InstanceOnlyDescriptor:
|
2017-06-05 05:58:24 +08:00
|
|
|
def __get__(self, instance, cls=None):
|
|
|
|
if instance is None:
|
|
|
|
raise AttributeError("Instance only")
|
|
|
|
return 1
|
|
|
|
|
|
|
|
|
2014-06-19 22:03:36 +08:00
|
|
|
class AbstractPerson(models.Model):
|
|
|
|
# DATA fields
|
|
|
|
data_abstract = models.CharField(max_length=10)
|
2015-07-22 22:43:21 +08:00
|
|
|
fk_abstract = models.ForeignKey(
|
|
|
|
Relation, models.CASCADE, related_name="fk_abstract_rel"
|
|
|
|
)
|
2014-06-19 22:03:36 +08:00
|
|
|
|
|
|
|
# M2M fields
|
|
|
|
m2m_abstract = models.ManyToManyField(Relation, related_name="m2m_abstract_rel")
|
2021-08-05 04:04:56 +08:00
|
|
|
friends_abstract = models.ManyToManyField("self", symmetrical=True)
|
2014-06-19 22:03:36 +08:00
|
|
|
following_abstract = models.ManyToManyField(
|
|
|
|
"self", related_name="followers_abstract", symmetrical=False
|
|
|
|
)
|
|
|
|
|
|
|
|
# VIRTUAL fields
|
|
|
|
data_not_concrete_abstract = models.ForeignObject(
|
|
|
|
Relation,
|
2015-07-22 22:43:21 +08:00
|
|
|
on_delete=models.CASCADE,
|
2014-06-19 22:03:36 +08:00
|
|
|
from_fields=["abstract_non_concrete_id"],
|
|
|
|
to_fields=["id"],
|
|
|
|
related_name="fo_abstract_rel",
|
|
|
|
)
|
|
|
|
|
|
|
|
# GFK fields
|
2015-07-22 22:43:21 +08:00
|
|
|
content_type_abstract = models.ForeignKey(
|
|
|
|
ContentType, models.CASCADE, related_name="+"
|
|
|
|
)
|
2014-06-19 22:03:36 +08:00
|
|
|
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
|
|
|
|
|
2017-06-05 20:27:55 +08:00
|
|
|
@property
|
|
|
|
def test_property(self):
|
|
|
|
return 1
|
|
|
|
|
2017-06-05 05:58:24 +08:00
|
|
|
test_instance_only_descriptor = InstanceOnlyDescriptor()
|
|
|
|
|
2014-06-19 22:03:36 +08:00
|
|
|
|
|
|
|
class BasePerson(AbstractPerson):
|
|
|
|
# DATA fields
|
|
|
|
data_base = models.CharField(max_length=10)
|
2015-07-22 22:43:21 +08:00
|
|
|
fk_base = models.ForeignKey(Relation, models.CASCADE, related_name="fk_base_rel")
|
2014-06-19 22:03:36 +08:00
|
|
|
|
|
|
|
# M2M fields
|
|
|
|
m2m_base = models.ManyToManyField(Relation, related_name="m2m_base_rel")
|
2021-08-05 04:04:56 +08:00
|
|
|
friends_base = models.ManyToManyField("self", symmetrical=True)
|
2014-06-19 22:03:36 +08:00
|
|
|
following_base = models.ManyToManyField(
|
|
|
|
"self", related_name="followers_base", symmetrical=False
|
|
|
|
)
|
|
|
|
|
|
|
|
# VIRTUAL fields
|
|
|
|
data_not_concrete_base = models.ForeignObject(
|
|
|
|
Relation,
|
2015-07-22 22:43:21 +08:00
|
|
|
on_delete=models.CASCADE,
|
2014-06-19 22:03:36 +08:00
|
|
|
from_fields=["base_non_concrete_id"],
|
|
|
|
to_fields=["id"],
|
|
|
|
related_name="fo_base_rel",
|
|
|
|
)
|
|
|
|
|
|
|
|
# GFK fields
|
2015-07-22 22:43:21 +08:00
|
|
|
content_type_base = models.ForeignKey(ContentType, models.CASCADE, related_name="+")
|
2014-06-19 22:03:36 +08:00
|
|
|
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)
|
2015-07-22 22:43:21 +08:00
|
|
|
fk_inherited = models.ForeignKey(
|
|
|
|
Relation, models.CASCADE, related_name="fk_concrete_rel"
|
|
|
|
)
|
2014-06-19 22:03:36 +08:00
|
|
|
|
|
|
|
# M2M Fields
|
|
|
|
m2m_inherited = models.ManyToManyField(Relation, related_name="m2m_concrete_rel")
|
2021-08-05 04:04:56 +08:00
|
|
|
friends_inherited = models.ManyToManyField("self", symmetrical=True)
|
2014-06-19 22:03:36 +08:00
|
|
|
following_inherited = models.ManyToManyField(
|
|
|
|
"self", related_name="followers_concrete", symmetrical=False
|
|
|
|
)
|
|
|
|
|
|
|
|
# VIRTUAL fields
|
|
|
|
data_not_concrete_inherited = models.ForeignObject(
|
|
|
|
Relation,
|
2015-07-22 22:43:21 +08:00
|
|
|
on_delete=models.CASCADE,
|
2014-06-19 22:03:36 +08:00
|
|
|
from_fields=["model_non_concrete_id"],
|
|
|
|
to_fields=["id"],
|
|
|
|
related_name="fo_concrete_rel",
|
|
|
|
)
|
|
|
|
|
|
|
|
# GFK fields
|
2015-07-22 22:43:21 +08:00
|
|
|
content_type_concrete = models.ForeignKey(
|
|
|
|
ContentType, models.CASCADE, related_name="+"
|
|
|
|
)
|
2014-06-19 22:03:36 +08:00
|
|
|
object_id_concrete = models.PositiveIntegerField()
|
|
|
|
content_object_concrete = GenericForeignKey(
|
|
|
|
"content_type_concrete", "object_id_concrete"
|
|
|
|
)
|
|
|
|
|
|
|
|
# GR fields
|
|
|
|
generic_relation_concrete = GenericRelation(Relation)
|
|
|
|
|
|
|
|
|
|
|
|
class ProxyPerson(Person):
|
|
|
|
class Meta:
|
|
|
|
proxy = True
|
|
|
|
|
|
|
|
|
2015-10-08 03:29:20 +08:00
|
|
|
class PersonThroughProxySubclass(ProxyPerson):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2014-06-19 22:03:36 +08:00
|
|
|
class Relating(models.Model):
|
|
|
|
# ForeignKey to BasePerson
|
2015-07-22 22:43:21 +08:00
|
|
|
baseperson = models.ForeignKey(
|
|
|
|
BasePerson, models.CASCADE, related_name="relating_baseperson"
|
|
|
|
)
|
|
|
|
baseperson_hidden = models.ForeignKey(BasePerson, models.CASCADE, related_name="+")
|
2014-06-19 22:03:36 +08:00
|
|
|
|
|
|
|
# ForeignKey to Person
|
2015-07-22 22:43:21 +08:00
|
|
|
person = models.ForeignKey(Person, models.CASCADE, related_name="relating_person")
|
|
|
|
person_hidden = models.ForeignKey(Person, models.CASCADE, related_name="+")
|
2014-06-19 22:03:36 +08:00
|
|
|
|
|
|
|
# ForeignKey to ProxyPerson
|
2015-07-22 22:43:21 +08:00
|
|
|
proxyperson = models.ForeignKey(
|
|
|
|
ProxyPerson, models.CASCADE, related_name="relating_proxyperson"
|
|
|
|
)
|
2015-10-02 02:57:58 +08:00
|
|
|
proxyperson_hidden = models.ForeignKey(
|
|
|
|
ProxyPerson, models.CASCADE, related_name="relating_proxyperson_hidden+"
|
|
|
|
)
|
2014-06-19 22:03:36 +08:00
|
|
|
|
|
|
|
# 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="+")
|
2015-02-04 04:13:43 +08:00
|
|
|
|
|
|
|
|
|
|
|
# ParentListTests models
|
|
|
|
class CommonAncestor(models.Model):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class FirstParent(CommonAncestor):
|
2016-03-18 22:24:29 +08:00
|
|
|
first_ancestor = models.OneToOneField(
|
|
|
|
CommonAncestor, models.CASCADE, primary_key=True, parent_link=True
|
|
|
|
)
|
2015-02-04 04:13:43 +08:00
|
|
|
|
|
|
|
|
|
|
|
class SecondParent(CommonAncestor):
|
2016-03-18 22:24:29 +08:00
|
|
|
second_ancestor = models.OneToOneField(
|
|
|
|
CommonAncestor, models.CASCADE, primary_key=True, parent_link=True
|
|
|
|
)
|
2015-02-04 04:13:43 +08:00
|
|
|
|
|
|
|
|
|
|
|
class Child(FirstParent, SecondParent):
|
|
|
|
pass
|