2 Various edge-cases for model managers.
5 from django
.db
import models
8 class OnlyFred(models
.Manager
):
9 def get_query_set(self
):
10 return super(OnlyFred
, self
).get_query_set().filter(name
='fred')
12 class OnlyBarney(models
.Manager
):
13 def get_query_set(self
):
14 return super(OnlyBarney
, self
).get_query_set().filter(name
='barney')
16 class Value42(models
.Manager
):
17 def get_query_set(self
):
18 return super(Value42
, self
).get_query_set().filter(value
=42)
20 class AbstractBase1(models
.Model
):
21 name
= models
.CharField(max_length
=50)
28 manager2
= OnlyBarney()
29 objects
= models
.Manager()
31 class AbstractBase2(models
.Model
):
32 value
= models
.IntegerField()
38 restricted
= Value42()
40 # No custom manager on this class to make sure the default case doesn't break.
41 class AbstractBase3(models
.Model
):
42 comment
= models
.CharField(max_length
=50)
47 class Parent(models
.Model
):
48 name
= models
.CharField(max_length
=50)
52 def __unicode__(self
):
55 # Managers from base classes are inherited and, if no manager is specified
56 # *and* the parent has a manager specified, the first one (in the MRO) will
58 class Child1(AbstractBase1
):
59 data
= models
.CharField(max_length
=25)
61 def __unicode__(self
):
64 class Child2(AbstractBase1
, AbstractBase2
):
65 data
= models
.CharField(max_length
=25)
67 def __unicode__(self
):
70 class Child3(AbstractBase1
, AbstractBase3
):
71 data
= models
.CharField(max_length
=25)
73 def __unicode__(self
):
76 class Child4(AbstractBase1
):
77 data
= models
.CharField(max_length
=25)
79 # Should be the default manager, although the parent managers are
81 default
= models
.Manager()
83 def __unicode__(self
):
86 class Child5(AbstractBase3
):
87 name
= models
.CharField(max_length
=25)
90 objects
= models
.Manager()
92 def __unicode__(self
):
95 # Will inherit managers from AbstractBase1, but not Child4.
97 value
= models
.IntegerField()
99 # Will not inherit default manager from parent.
100 class Child7(Parent
):