PageRenderTime 51ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/regressiontests/managers_regress/models.py

https://code.google.com/p/mango-py/
Python | 100 lines | 58 code | 29 blank | 13 comment | 0 complexity | 4058d229faaaabbe391dd1800c8f580a MD5 | raw file
Possible License(s): BSD-3-Clause
  1. """
  2. Various edge-cases for model managers.
  3. """
  4. from django.db import models
  5. class OnlyFred(models.Manager):
  6. def get_query_set(self):
  7. return super(OnlyFred, self).get_query_set().filter(name='fred')
  8. class OnlyBarney(models.Manager):
  9. def get_query_set(self):
  10. return super(OnlyBarney, self).get_query_set().filter(name='barney')
  11. class Value42(models.Manager):
  12. def get_query_set(self):
  13. return super(Value42, self).get_query_set().filter(value=42)
  14. class AbstractBase1(models.Model):
  15. name = models.CharField(max_length=50)
  16. class Meta:
  17. abstract = True
  18. # Custom managers
  19. manager1 = OnlyFred()
  20. manager2 = OnlyBarney()
  21. objects = models.Manager()
  22. class AbstractBase2(models.Model):
  23. value = models.IntegerField()
  24. class Meta:
  25. abstract = True
  26. # Custom manager
  27. restricted = Value42()
  28. # No custom manager on this class to make sure the default case doesn't break.
  29. class AbstractBase3(models.Model):
  30. comment = models.CharField(max_length=50)
  31. class Meta:
  32. abstract = True
  33. class Parent(models.Model):
  34. name = models.CharField(max_length=50)
  35. manager = OnlyFred()
  36. def __unicode__(self):
  37. return self.name
  38. # Managers from base classes are inherited and, if no manager is specified
  39. # *and* the parent has a manager specified, the first one (in the MRO) will
  40. # become the default.
  41. class Child1(AbstractBase1):
  42. data = models.CharField(max_length=25)
  43. def __unicode__(self):
  44. return self.data
  45. class Child2(AbstractBase1, AbstractBase2):
  46. data = models.CharField(max_length=25)
  47. def __unicode__(self):
  48. return self.data
  49. class Child3(AbstractBase1, AbstractBase3):
  50. data = models.CharField(max_length=25)
  51. def __unicode__(self):
  52. return self.data
  53. class Child4(AbstractBase1):
  54. data = models.CharField(max_length=25)
  55. # Should be the default manager, although the parent managers are
  56. # inherited.
  57. default = models.Manager()
  58. def __unicode__(self):
  59. return self.data
  60. class Child5(AbstractBase3):
  61. name = models.CharField(max_length=25)
  62. default = OnlyFred()
  63. objects = models.Manager()
  64. def __unicode__(self):
  65. return self.name
  66. # Will inherit managers from AbstractBase1, but not Child4.
  67. class Child6(Child4):
  68. value = models.IntegerField()
  69. # Will not inherit default manager from parent.
  70. class Child7(Parent):
  71. pass