models.py 4.6 KB

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