PageRenderTime 102ms CodeModel.GetById 81ms app.highlight 6ms RepoModel.GetById 14ms 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
  1"""
  2Various edge-cases for model managers.
  3"""
  4
  5from django.db import models
  6
  7class OnlyFred(models.Manager):
  8    def get_query_set(self):
  9        return super(OnlyFred, self).get_query_set().filter(name='fred')
 10
 11class OnlyBarney(models.Manager):
 12    def get_query_set(self):
 13        return super(OnlyBarney, self).get_query_set().filter(name='barney')
 14
 15class Value42(models.Manager):
 16    def get_query_set(self):
 17        return super(Value42, self).get_query_set().filter(value=42)
 18
 19class AbstractBase1(models.Model):
 20    name = models.CharField(max_length=50)
 21
 22    class Meta:
 23        abstract = True
 24
 25    # Custom managers
 26    manager1 = OnlyFred()
 27    manager2 = OnlyBarney()
 28    objects = models.Manager()
 29
 30class AbstractBase2(models.Model):
 31    value = models.IntegerField()
 32
 33    class Meta:
 34        abstract = True
 35
 36    # Custom manager
 37    restricted = Value42()
 38
 39# No custom manager on this class to make sure the default case doesn't break.
 40class AbstractBase3(models.Model):
 41    comment = models.CharField(max_length=50)
 42
 43    class Meta:
 44        abstract = True
 45
 46class Parent(models.Model):
 47    name = models.CharField(max_length=50)
 48
 49    manager = OnlyFred()
 50
 51    def __unicode__(self):
 52        return self.name
 53
 54# Managers from base classes are inherited and, if no manager is specified
 55# *and* the parent has a manager specified, the first one (in the MRO) will
 56# become the default.
 57class Child1(AbstractBase1):
 58    data = models.CharField(max_length=25)
 59
 60    def __unicode__(self):
 61        return self.data
 62
 63class Child2(AbstractBase1, AbstractBase2):
 64    data = models.CharField(max_length=25)
 65
 66    def __unicode__(self):
 67        return self.data
 68
 69class Child3(AbstractBase1, AbstractBase3):
 70    data = models.CharField(max_length=25)
 71
 72    def __unicode__(self):
 73        return self.data
 74
 75class Child4(AbstractBase1):
 76    data = models.CharField(max_length=25)
 77
 78    # Should be the default manager, although the parent managers are
 79    # inherited.
 80    default = models.Manager()
 81
 82    def __unicode__(self):
 83        return self.data
 84
 85class Child5(AbstractBase3):
 86    name = models.CharField(max_length=25)
 87
 88    default = OnlyFred()
 89    objects = models.Manager()
 90
 91    def __unicode__(self):
 92        return self.name
 93
 94# Will inherit managers from AbstractBase1, but not Child4.
 95class Child6(Child4):
 96    value = models.IntegerField()
 97
 98# Will not inherit default manager from parent.
 99class Child7(Parent):
100    pass