parts/django/tests/regressiontests/managers_regress/models.py
changeset 307 c6bca38c1cbf
equal deleted inserted replaced
306:5ff1fc726848 307:c6bca38c1cbf
       
     1 """
       
     2 Various edge-cases for model managers.
       
     3 """
       
     4 
       
     5 from django.db import models
       
     6 
       
     7 class OnlyFred(models.Manager):
       
     8     def get_query_set(self):
       
     9         return super(OnlyFred, self).get_query_set().filter(name='fred')
       
    10 
       
    11 class OnlyBarney(models.Manager):
       
    12     def get_query_set(self):
       
    13         return super(OnlyBarney, self).get_query_set().filter(name='barney')
       
    14 
       
    15 class Value42(models.Manager):
       
    16     def get_query_set(self):
       
    17         return super(Value42, self).get_query_set().filter(value=42)
       
    18 
       
    19 class 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 
       
    30 class 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.
       
    40 class AbstractBase3(models.Model):
       
    41     comment = models.CharField(max_length=50)
       
    42 
       
    43     class Meta:
       
    44         abstract = True
       
    45 
       
    46 class 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.
       
    57 class Child1(AbstractBase1):
       
    58     data = models.CharField(max_length=25)
       
    59 
       
    60     def __unicode__(self):
       
    61         return self.data
       
    62 
       
    63 class Child2(AbstractBase1, AbstractBase2):
       
    64     data = models.CharField(max_length=25)
       
    65 
       
    66     def __unicode__(self):
       
    67         return self.data
       
    68 
       
    69 class Child3(AbstractBase1, AbstractBase3):
       
    70     data = models.CharField(max_length=25)
       
    71 
       
    72     def __unicode__(self):
       
    73         return self.data
       
    74 
       
    75 class 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 
       
    85 class 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.
       
    95 class Child6(Child4):
       
    96     value = models.IntegerField()
       
    97 
       
    98 # Will not inherit default manager from parent.
       
    99 class Child7(Parent):
       
   100     pass