models.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. from django.contrib.contenttypes.fields import (
  2. GenericForeignKey, GenericRelation,
  3. )
  4. from django.contrib.contenttypes.models import ContentType
  5. from django.db import models
  6. class Relation(models.Model):
  7. pass
  8. class InstanceOnlyDescriptor:
  9. def __get__(self, instance, cls=None):
  10. if instance is None:
  11. raise AttributeError('Instance only')
  12. return 1
  13. class AbstractPerson(models.Model):
  14. # DATA fields
  15. data_abstract = models.CharField(max_length=10)
  16. fk_abstract = models.ForeignKey(Relation, models.CASCADE, related_name='fk_abstract_rel')
  17. # M2M fields
  18. m2m_abstract = models.ManyToManyField(Relation, related_name='m2m_abstract_rel')
  19. friends_abstract = models.ManyToManyField('self', related_name='friends_abstract', symmetrical=True)
  20. following_abstract = models.ManyToManyField('self', related_name='followers_abstract', symmetrical=False)
  21. # VIRTUAL fields
  22. data_not_concrete_abstract = models.ForeignObject(
  23. Relation,
  24. on_delete=models.CASCADE,
  25. from_fields=['abstract_non_concrete_id'],
  26. to_fields=['id'],
  27. related_name='fo_abstract_rel',
  28. )
  29. # GFK fields
  30. content_type_abstract = models.ForeignKey(ContentType, models.CASCADE, related_name='+')
  31. object_id_abstract = models.PositiveIntegerField()
  32. content_object_abstract = GenericForeignKey('content_type_abstract', 'object_id_abstract')
  33. # GR fields
  34. generic_relation_abstract = GenericRelation(Relation)
  35. class Meta:
  36. abstract = True
  37. @property
  38. def test_property(self):
  39. return 1
  40. test_instance_only_descriptor = InstanceOnlyDescriptor()
  41. class BasePerson(AbstractPerson):
  42. # DATA fields
  43. data_base = models.CharField(max_length=10)
  44. fk_base = models.ForeignKey(Relation, models.CASCADE, related_name='fk_base_rel')
  45. # M2M fields
  46. m2m_base = models.ManyToManyField(Relation, related_name='m2m_base_rel')
  47. friends_base = models.ManyToManyField('self', related_name='friends_base', symmetrical=True)
  48. following_base = models.ManyToManyField('self', related_name='followers_base', symmetrical=False)
  49. # VIRTUAL fields
  50. data_not_concrete_base = models.ForeignObject(
  51. Relation,
  52. on_delete=models.CASCADE,
  53. from_fields=['base_non_concrete_id'],
  54. to_fields=['id'],
  55. related_name='fo_base_rel',
  56. )
  57. # GFK fields
  58. content_type_base = models.ForeignKey(ContentType, models.CASCADE, related_name='+')
  59. object_id_base = models.PositiveIntegerField()
  60. content_object_base = GenericForeignKey('content_type_base', 'object_id_base')
  61. # GR fields
  62. generic_relation_base = GenericRelation(Relation)
  63. class Person(BasePerson):
  64. # DATA fields
  65. data_inherited = models.CharField(max_length=10)
  66. fk_inherited = models.ForeignKey(Relation, models.CASCADE, related_name='fk_concrete_rel')
  67. # M2M Fields
  68. m2m_inherited = models.ManyToManyField(Relation, related_name='m2m_concrete_rel')
  69. friends_inherited = models.ManyToManyField('self', related_name='friends_concrete', symmetrical=True)
  70. following_inherited = models.ManyToManyField('self', related_name='followers_concrete', symmetrical=False)
  71. # VIRTUAL fields
  72. data_not_concrete_inherited = models.ForeignObject(
  73. Relation,
  74. on_delete=models.CASCADE,
  75. from_fields=['model_non_concrete_id'],
  76. to_fields=['id'],
  77. related_name='fo_concrete_rel',
  78. )
  79. # GFK fields
  80. content_type_concrete = models.ForeignKey(ContentType, models.CASCADE, related_name='+')
  81. object_id_concrete = models.PositiveIntegerField()
  82. content_object_concrete = GenericForeignKey('content_type_concrete', 'object_id_concrete')
  83. # GR fields
  84. generic_relation_concrete = GenericRelation(Relation)
  85. class ProxyPerson(Person):
  86. class Meta:
  87. proxy = True
  88. class PersonThroughProxySubclass(ProxyPerson):
  89. pass
  90. class Relating(models.Model):
  91. # ForeignKey to BasePerson
  92. baseperson = models.ForeignKey(BasePerson, models.CASCADE, related_name='relating_baseperson')
  93. baseperson_hidden = models.ForeignKey(BasePerson, models.CASCADE, related_name='+')
  94. # ForeignKey to Person
  95. person = models.ForeignKey(Person, models.CASCADE, related_name='relating_person')
  96. person_hidden = models.ForeignKey(Person, models.CASCADE, related_name='+')
  97. # ForeignKey to ProxyPerson
  98. proxyperson = models.ForeignKey(ProxyPerson, models.CASCADE, related_name='relating_proxyperson')
  99. proxyperson_hidden = models.ForeignKey(ProxyPerson, models.CASCADE, related_name='relating_proxyperson_hidden+')
  100. # ManyToManyField to BasePerson
  101. basepeople = models.ManyToManyField(BasePerson, related_name='relating_basepeople')
  102. basepeople_hidden = models.ManyToManyField(BasePerson, related_name='+')
  103. # ManyToManyField to Person
  104. people = models.ManyToManyField(Person, related_name='relating_people')
  105. people_hidden = models.ManyToManyField(Person, related_name='+')
  106. # ParentListTests models
  107. class CommonAncestor(models.Model):
  108. pass
  109. class FirstParent(CommonAncestor):
  110. first_ancestor = models.OneToOneField(CommonAncestor, models.CASCADE, primary_key=True, parent_link=True)
  111. class SecondParent(CommonAncestor):
  112. second_ancestor = models.OneToOneField(CommonAncestor, models.CASCADE, primary_key=True, parent_link=True)
  113. class Child(FirstParent, SecondParent):
  114. pass