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 GenericForeignKey, GenericRelation
|
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
|
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):
|
2017-01-21 21:13:44 +08:00
|
|
|
return super().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):
|
2017-01-21 21:13:44 +08:00
|
|
|
return super().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):
|
2017-01-21 21:13:44 +08:00
|
|
|
return super().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
|
|
|
|
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.
|
|
|
|
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
|
|
|
|
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
|
|
|
|
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
|
|
|
|
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
|
|
|
|
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
|
|
|
class Child6(Child4):
|
|
|
|
value = models.IntegerField()
|
|
|
|
|
2012-10-06 14:21:57 +08:00
|
|
|
|
2008-09-02 17:04:54 +08:00
|
|
|
class Child7(Parent):
|
2016-02-19 03:27:55 +08:00
|
|
|
objects = models.Manager()
|
2013-09-30 11:21:21 +08:00
|
|
|
|
|
|
|
|
|
|
|
# RelatedManagers
|
|
|
|
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"
|
|
|
|
)
|
2017-05-06 22:56:28 +08:00
|
|
|
exact = models.BooleanField(null=True)
|
2013-09-30 11:21:21 +08:00
|
|
|
|
|
|
|
def __str__(self):
|
2017-01-24 19:22:42 +08:00
|
|
|
return str(self.pk)
|
2013-09-30 11:21:21 +08:00
|
|
|
|
|
|
|
|
|
|
|
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):
|
2017-01-24 19:22:42 +08:00
|
|
|
return str(self.pk)
|