PageRenderTime 24ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/modeltests/proxy_models/models.py

https://code.google.com/p/mango-py/
Python | 164 lines | 163 code | 0 blank | 1 comment | 0 complexity | 2e02d2bdf3ca7ccea691209cf37640f4 MD5 | raw file
Possible License(s): BSD-3-Clause
  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.contrib.contenttypes.models import ContentType
  8. from django.db import models
  9. # A couple of managers for testing managing overriding in proxy model cases.
  10. class PersonManager(models.Manager):
  11. def get_query_set(self):
  12. return super(PersonManager, self).get_query_set().exclude(name="fred")
  13. class SubManager(models.Manager):
  14. def get_query_set(self):
  15. return super(SubManager, self).get_query_set().exclude(name="wilma")
  16. class Person(models.Model):
  17. """
  18. A simple concrete base class.
  19. """
  20. name = models.CharField(max_length=50)
  21. objects = PersonManager()
  22. def __unicode__(self):
  23. return self.name
  24. class Abstract(models.Model):
  25. """
  26. A simple abstract base class, to be used for error checking.
  27. """
  28. data = models.CharField(max_length=10)
  29. class Meta:
  30. abstract = True
  31. class MyPerson(Person):
  32. """
  33. A proxy subclass, this should not get a new table. Overrides the default
  34. manager.
  35. """
  36. class Meta:
  37. proxy = True
  38. ordering = ["name"]
  39. objects = SubManager()
  40. other = PersonManager()
  41. def has_special_name(self):
  42. return self.name.lower() == "special"
  43. class ManagerMixin(models.Model):
  44. excluder = SubManager()
  45. class Meta:
  46. abstract = True
  47. class OtherPerson(Person, ManagerMixin):
  48. """
  49. A class with the default manager from Person, plus an secondary manager.
  50. """
  51. class Meta:
  52. proxy = True
  53. ordering = ["name"]
  54. class StatusPerson(MyPerson):
  55. """
  56. A non-proxy subclass of a proxy, it should get a new table.
  57. """
  58. status = models.CharField(max_length=80)
  59. # We can even have proxies of proxies (and subclass of those).
  60. class MyPersonProxy(MyPerson):
  61. class Meta:
  62. proxy = True
  63. class LowerStatusPerson(MyPersonProxy):
  64. status = models.CharField(max_length=80)
  65. class User(models.Model):
  66. name = models.CharField(max_length=100)
  67. def __unicode__(self):
  68. return self.name
  69. class UserProxy(User):
  70. class Meta:
  71. proxy = True
  72. class UserProxyProxy(UserProxy):
  73. class Meta:
  74. proxy = True
  75. # We can still use `select_related()` to include related models in our querysets.
  76. class Country(models.Model):
  77. name = models.CharField(max_length=50)
  78. class State(models.Model):
  79. name = models.CharField(max_length=50)
  80. country = models.ForeignKey(Country)
  81. def __unicode__(self):
  82. return self.name
  83. class StateProxy(State):
  84. class Meta:
  85. proxy = True
  86. # Proxy models still works with filters (on related fields)
  87. # and select_related, even when mixed with model inheritance
  88. class BaseUser(models.Model):
  89. name = models.CharField(max_length=255)
  90. class TrackerUser(BaseUser):
  91. status = models.CharField(max_length=50)
  92. class ProxyTrackerUser(TrackerUser):
  93. class Meta:
  94. proxy = True
  95. class Issue(models.Model):
  96. summary = models.CharField(max_length=255)
  97. assignee = models.ForeignKey(TrackerUser)
  98. def __unicode__(self):
  99. return ':'.join((self.__class__.__name__,self.summary,))
  100. class Bug(Issue):
  101. version = models.CharField(max_length=50)
  102. reporter = models.ForeignKey(BaseUser)
  103. class ProxyBug(Bug):
  104. """
  105. Proxy of an inherited class
  106. """
  107. class Meta:
  108. proxy = True
  109. class ProxyProxyBug(ProxyBug):
  110. """
  111. A proxy of proxy model with related field
  112. """
  113. class Meta:
  114. proxy = True
  115. class Improvement(Issue):
  116. """
  117. A model that has relation to a proxy model
  118. or to a proxy of proxy model
  119. """
  120. version = models.CharField(max_length=50)
  121. reporter = models.ForeignKey(ProxyTrackerUser)
  122. associated_bug = models.ForeignKey(ProxyProxyBug)
  123. class ProxyImprovement(Improvement):
  124. class Meta:
  125. proxy = True