models.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. """
  2. By specifying the 'proxy' Meta attribute, model subclasses can specify that
  3. they will take data directly from the table of their base class table rather
  4. than using a new table of their own. This allows them to act as simple proxies,
  5. providing a modified interface to the data from the base class.
  6. """
  7. from django.db import models
  8. # A couple of managers for testing managing overriding in proxy model cases.
  9. class PersonManager(models.Manager):
  10. def get_queryset(self):
  11. return super().get_queryset().exclude(name="fred")
  12. class SubManager(models.Manager):
  13. def get_queryset(self):
  14. return super().get_queryset().exclude(name="wilma")
  15. class Person(models.Model):
  16. """
  17. A simple concrete base class.
  18. """
  19. name = models.CharField(max_length=50)
  20. objects = PersonManager()
  21. def __str__(self):
  22. return self.name
  23. class Abstract(models.Model):
  24. """
  25. A simple abstract base class, to be used for error checking.
  26. """
  27. data = models.CharField(max_length=10)
  28. class Meta:
  29. abstract = True
  30. class MyPerson(Person):
  31. """
  32. A proxy subclass, this should not get a new table. Overrides the default
  33. manager.
  34. """
  35. class Meta:
  36. proxy = True
  37. ordering = ["name"]
  38. permissions = (
  39. ("display_users", "May display users information"),
  40. )
  41. objects = SubManager()
  42. other = PersonManager()
  43. def has_special_name(self):
  44. return self.name.lower() == "special"
  45. class ManagerMixin(models.Model):
  46. excluder = SubManager()
  47. class Meta:
  48. abstract = True
  49. class OtherPerson(Person, ManagerMixin):
  50. """
  51. A class with the default manager from Person, plus a secondary manager.
  52. """
  53. class Meta:
  54. proxy = True
  55. ordering = ["name"]
  56. class StatusPerson(MyPerson):
  57. """
  58. A non-proxy subclass of a proxy, it should get a new table.
  59. """
  60. status = models.CharField(max_length=80)
  61. objects = models.Manager()
  62. # We can even have proxies of proxies (and subclass of those).
  63. class MyPersonProxy(MyPerson):
  64. class Meta:
  65. proxy = True
  66. class LowerStatusPerson(MyPersonProxy):
  67. status = models.CharField(max_length=80)
  68. objects = models.Manager()
  69. class User(models.Model):
  70. name = models.CharField(max_length=100)
  71. def __str__(self):
  72. return self.name
  73. class UserProxy(User):
  74. class Meta:
  75. proxy = True
  76. class AnotherUserProxy(User):
  77. class Meta:
  78. proxy = True
  79. class UserProxyProxy(UserProxy):
  80. class Meta:
  81. proxy = True
  82. class MultiUserProxy(UserProxy, AnotherUserProxy):
  83. class Meta:
  84. proxy = True
  85. # We can still use `select_related()` to include related models in our querysets.
  86. class Country(models.Model):
  87. name = models.CharField(max_length=50)
  88. class State(models.Model):
  89. name = models.CharField(max_length=50)
  90. country = models.ForeignKey(Country, models.CASCADE)
  91. def __str__(self):
  92. return self.name
  93. class StateProxy(State):
  94. class Meta:
  95. proxy = True
  96. # Proxy models still works with filters (on related fields)
  97. # and select_related, even when mixed with model inheritance
  98. class BaseUser(models.Model):
  99. name = models.CharField(max_length=255)
  100. def __str__(self):
  101. return ':'.join((self.__class__.__name__, self.name,))
  102. class TrackerUser(BaseUser):
  103. status = models.CharField(max_length=50)
  104. class ProxyTrackerUser(TrackerUser):
  105. class Meta:
  106. proxy = True
  107. class Issue(models.Model):
  108. summary = models.CharField(max_length=255)
  109. assignee = models.ForeignKey(ProxyTrackerUser, models.CASCADE, related_name='issues')
  110. def __str__(self):
  111. return ':'.join((self.__class__.__name__, self.summary,))
  112. class Bug(Issue):
  113. version = models.CharField(max_length=50)
  114. reporter = models.ForeignKey(BaseUser, models.CASCADE)
  115. class ProxyBug(Bug):
  116. """
  117. Proxy of an inherited class
  118. """
  119. class Meta:
  120. proxy = True
  121. class ProxyProxyBug(ProxyBug):
  122. """
  123. A proxy of proxy model with related field
  124. """
  125. class Meta:
  126. proxy = True
  127. class Improvement(Issue):
  128. """
  129. A model that has relation to a proxy model
  130. or to a proxy of proxy model
  131. """
  132. version = models.CharField(max_length=50)
  133. reporter = models.ForeignKey(ProxyTrackerUser, models.CASCADE)
  134. associated_bug = models.ForeignKey(ProxyProxyBug, models.CASCADE)
  135. class ProxyImprovement(Improvement):
  136. class Meta:
  137. proxy = True