2008-09-02 17:04:54 +08:00
|
|
|
"""
|
|
|
|
Various edge-cases for model managers.
|
|
|
|
"""
|
|
|
|
|
2014-01-22 14:43:33 +08:00
|
|
|
from django.contrib.contenttypes.fields import (
|
2015-01-28 20:35:27 +08:00
|
|
|
GenericForeignKey, GenericRelation,
|
2014-01-22 14:43:33 +08:00
|
|
|
)
|
2013-09-30 11:21:21 +08:00
|
|
|
from django.contrib.contenttypes.models import ContentType
|
2014-01-22 14:43:33 +08:00
|
|
|
from django.db import models
|
2015-01-28 20:35:27 +08:00
|
|
|
from django.utils.encoding import force_text, python_2_unicode_compatible
|
2008-09-02 17:04:54 +08:00
|
|
|
|
2011-10-14 05:34:56 +08:00
|
|
|
|
2008-09-02 17:04:54 +08:00
|
|
|
class OnlyFred(models.Manager):
|
2013-03-08 22:15:23 +08:00
|
|
|
def get_queryset(self):
|
|
|
|
return super(OnlyFred, self).get_queryset().filter(name='fred')
|
2008-09-02 17:04:54 +08:00
|
|
|
|
2012-10-06 14:21:57 +08:00
|
|
|
|
2008-09-02 17:04:54 +08:00
|
|
|
class OnlyBarney(models.Manager):
|
2013-03-08 22:15:23 +08:00
|
|
|
def get_queryset(self):
|
|
|
|
return super(OnlyBarney, self).get_queryset().filter(name='barney')
|
2008-09-02 17:04:54 +08:00
|
|
|
|
2012-10-06 14:21:57 +08:00
|
|
|
|
2008-09-02 17:04:54 +08:00
|
|
|
class Value42(models.Manager):
|
2013-03-08 22:15:23 +08:00
|
|
|
def get_queryset(self):
|
|
|
|
return super(Value42, self).get_queryset().filter(value=42)
|
2008-09-02 17:04:54 +08:00
|
|
|
|
2012-10-06 14:21:57 +08:00
|
|
|
|
2008-09-02 17:04:54 +08:00
|
|
|
class AbstractBase1(models.Model):
|
|
|
|
name = models.CharField(max_length=50)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
abstract = True
|
|
|
|
|
|
|
|
# Custom managers
|
|
|
|
manager1 = OnlyFred()
|
|
|
|
manager2 = OnlyBarney()
|
|
|
|
objects = models.Manager()
|
|
|
|
|
2012-10-06 14:21:57 +08:00
|
|
|
|
2008-09-02 17:04:54 +08:00
|
|
|
class AbstractBase2(models.Model):
|
|
|
|
value = models.IntegerField()
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
abstract = True
|
|
|
|
|
|
|
|
# Custom manager
|
|
|
|
restricted = Value42()
|
|
|
|
|
2012-10-06 14:21:57 +08:00
|
|
|
|
2008-09-02 17:04:54 +08:00
|
|
|
# No custom manager on this class to make sure the default case doesn't break.
|
|
|
|
class AbstractBase3(models.Model):
|
|
|
|
comment = models.CharField(max_length=50)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
abstract = True
|
|
|
|
|
2012-10-06 14:21:57 +08:00
|
|
|
|
2012-08-12 18:32:08 +08:00
|
|
|
@python_2_unicode_compatible
|
2008-09-02 17:04:54 +08:00
|
|
|
class Parent(models.Model):
|
|
|
|
name = models.CharField(max_length=50)
|
|
|
|
|
|
|
|
manager = OnlyFred()
|
|
|
|
|
2012-08-12 18:32:08 +08:00
|
|
|
def __str__(self):
|
2008-09-02 17:04:54 +08:00
|
|
|
return self.name
|
|
|
|
|
2012-10-06 14:21:57 +08:00
|
|
|
|
2008-09-02 17:04:54 +08:00
|
|
|
# Managers from base classes are inherited and, if no manager is specified
|
|
|
|
# *and* the parent has a manager specified, the first one (in the MRO) will
|
|
|
|
# become the default.
|
2012-08-12 18:32:08 +08:00
|
|
|
@python_2_unicode_compatible
|
2008-09-02 17:04:54 +08:00
|
|
|
class Child1(AbstractBase1):
|
|
|
|
data = models.CharField(max_length=25)
|
|
|
|
|
2012-08-12 18:32:08 +08:00
|
|
|
def __str__(self):
|
2008-09-02 17:04:54 +08:00
|
|
|
return self.data
|
|
|
|
|
2012-10-06 14:21:57 +08:00
|
|
|
|
2012-08-12 18:32:08 +08:00
|
|
|
@python_2_unicode_compatible
|
2008-09-02 17:04:54 +08:00
|
|
|
class Child2(AbstractBase1, AbstractBase2):
|
|
|
|
data = models.CharField(max_length=25)
|
|
|
|
|
2012-08-12 18:32:08 +08:00
|
|
|
def __str__(self):
|
2008-09-02 17:04:54 +08:00
|
|
|
return self.data
|
|
|
|
|
2012-10-06 14:21:57 +08:00
|
|
|
|
2012-08-12 18:32:08 +08:00
|
|
|
@python_2_unicode_compatible
|
2008-09-02 17:04:54 +08:00
|
|
|
class Child3(AbstractBase1, AbstractBase3):
|
|
|
|
data = models.CharField(max_length=25)
|
|
|
|
|
2012-08-12 18:32:08 +08:00
|
|
|
def __str__(self):
|
2008-09-02 17:04:54 +08:00
|
|
|
return self.data
|
|
|
|
|
2012-10-06 14:21:57 +08:00
|
|
|
|
2012-08-12 18:32:08 +08:00
|
|
|
@python_2_unicode_compatible
|
2008-09-02 17:04:54 +08:00
|
|
|
class Child4(AbstractBase1):
|
|
|
|
data = models.CharField(max_length=25)
|
|
|
|
|
|
|
|
# Should be the default manager, although the parent managers are
|
|
|
|
# inherited.
|
|
|
|
default = models.Manager()
|
|
|
|
|
2012-08-12 18:32:08 +08:00
|
|
|
def __str__(self):
|
2008-09-02 17:04:54 +08:00
|
|
|
return self.data
|
|
|
|
|
2012-10-06 14:21:57 +08:00
|
|
|
|
2012-08-12 18:32:08 +08:00
|
|
|
@python_2_unicode_compatible
|
2008-09-02 17:04:54 +08:00
|
|
|
class Child5(AbstractBase3):
|
|
|
|
name = models.CharField(max_length=25)
|
|
|
|
|
|
|
|
default = OnlyFred()
|
|
|
|
objects = models.Manager()
|
|
|
|
|
2012-08-12 18:32:08 +08:00
|
|
|
def __str__(self):
|
2008-09-02 17:04:54 +08:00
|
|
|
return self.name
|
|
|
|
|
2012-10-06 14:21:57 +08:00
|
|
|
|
2008-09-02 17:04:54 +08:00
|
|
|
# Will inherit managers from AbstractBase1, but not Child4.
|
|
|
|
class Child6(Child4):
|
|
|
|
value = models.IntegerField()
|
|
|
|
|
2012-10-06 14:21:57 +08:00
|
|
|
|
2008-09-02 17:04:54 +08:00
|
|
|
# Will not inherit default manager from parent.
|
|
|
|
class Child7(Parent):
|
|
|
|
pass
|
2013-09-30 11:21:21 +08:00
|
|
|
|
|
|
|
|
|
|
|
# RelatedManagers
|
|
|
|
@python_2_unicode_compatible
|
|
|
|
class RelatedModel(models.Model):
|
2014-01-22 14:43:33 +08:00
|
|
|
test_gfk = GenericRelation('RelationModel', content_type_field='gfk_ctype', object_id_field='gfk_id')
|
2015-01-22 14:43:49 +08:00
|
|
|
exact = models.NullBooleanField()
|
2013-09-30 11:21:21 +08:00
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return force_text(self.pk)
|
|
|
|
|
|
|
|
|
|
|
|
@python_2_unicode_compatible
|
|
|
|
class RelationModel(models.Model):
|
2015-07-22 22:43:21 +08:00
|
|
|
fk = models.ForeignKey(RelatedModel, models.CASCADE, related_name='test_fk')
|
2013-09-30 11:21:21 +08:00
|
|
|
|
|
|
|
m2m = models.ManyToManyField(RelatedModel, related_name='test_m2m')
|
|
|
|
|
2015-07-22 22:43:21 +08:00
|
|
|
gfk_ctype = models.ForeignKey(ContentType, models.SET_NULL, null=True)
|
2015-01-22 14:43:49 +08:00
|
|
|
gfk_id = models.IntegerField(null=True)
|
2014-01-22 14:43:33 +08:00
|
|
|
gfk = GenericForeignKey(ct_field='gfk_ctype', fk_field='gfk_id')
|
2013-09-30 11:21:21 +08:00
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return force_text(self.pk)
|