2008-09-02 09:04:54 +00:00
|
|
|
"""
|
|
|
|
Various edge-cases for model managers.
|
|
|
|
"""
|
|
|
|
|
2022-02-03 19:24:19 +00:00
|
|
|
from django.contrib.contenttypes.fields import GenericForeignKey, GenericRelation
|
2013-09-30 03:21:21 +00:00
|
|
|
from django.contrib.contenttypes.models import ContentType
|
2014-01-22 06:43:33 +00:00
|
|
|
from django.db import models
|
2008-09-02 09:04:54 +00:00
|
|
|
|
2011-10-13 21:34:56 +00:00
|
|
|
|
2008-09-02 09:04:54 +00:00
|
|
|
class OnlyFred(models.Manager):
|
2013-03-08 14:15:23 +00:00
|
|
|
def get_queryset(self):
|
2022-02-03 19:24:19 +00:00
|
|
|
return super().get_queryset().filter(name="fred")
|
2008-09-02 09:04:54 +00:00
|
|
|
|
2012-10-06 06:21:57 +00:00
|
|
|
|
2008-09-02 09:04:54 +00:00
|
|
|
class OnlyBarney(models.Manager):
|
2013-03-08 14:15:23 +00:00
|
|
|
def get_queryset(self):
|
2022-02-03 19:24:19 +00:00
|
|
|
return super().get_queryset().filter(name="barney")
|
2008-09-02 09:04:54 +00:00
|
|
|
|
2012-10-06 06:21:57 +00:00
|
|
|
|
2008-09-02 09:04:54 +00:00
|
|
|
class Value42(models.Manager):
|
2013-03-08 14:15:23 +00:00
|
|
|
def get_queryset(self):
|
2017-01-21 13:13:44 +00:00
|
|
|
return super().get_queryset().filter(value=42)
|
2008-09-02 09:04:54 +00:00
|
|
|
|
2012-10-06 06:21:57 +00:00
|
|
|
|
2008-09-02 09:04:54 +00: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 06:21:57 +00:00
|
|
|
|
2008-09-02 09:04:54 +00:00
|
|
|
class AbstractBase2(models.Model):
|
|
|
|
value = models.IntegerField()
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
abstract = True
|
|
|
|
|
|
|
|
# Custom manager
|
|
|
|
restricted = Value42()
|
|
|
|
|
2012-10-06 06:21:57 +00:00
|
|
|
|
2008-09-02 09:04:54 +00: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 06:21:57 +00:00
|
|
|
|
2008-09-02 09:04:54 +00:00
|
|
|
class Parent(models.Model):
|
|
|
|
name = models.CharField(max_length=50)
|
|
|
|
|
|
|
|
manager = OnlyFred()
|
|
|
|
|
2012-08-12 10:32:08 +00:00
|
|
|
def __str__(self):
|
2008-09-02 09:04:54 +00:00
|
|
|
return self.name
|
|
|
|
|
2012-10-06 06:21:57 +00:00
|
|
|
|
2008-09-02 09:04:54 +00: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 10:32:08 +00:00
|
|
|
def __str__(self):
|
2008-09-02 09:04:54 +00:00
|
|
|
return self.data
|
|
|
|
|
2012-10-06 06:21:57 +00:00
|
|
|
|
2008-09-02 09:04:54 +00:00
|
|
|
class Child2(AbstractBase1, AbstractBase2):
|
|
|
|
data = models.CharField(max_length=25)
|
|
|
|
|
2012-08-12 10:32:08 +00:00
|
|
|
def __str__(self):
|
2008-09-02 09:04:54 +00:00
|
|
|
return self.data
|
|
|
|
|
2012-10-06 06:21:57 +00:00
|
|
|
|
2008-09-02 09:04:54 +00:00
|
|
|
class Child3(AbstractBase1, AbstractBase3):
|
|
|
|
data = models.CharField(max_length=25)
|
|
|
|
|
2012-08-12 10:32:08 +00:00
|
|
|
def __str__(self):
|
2008-09-02 09:04:54 +00:00
|
|
|
return self.data
|
|
|
|
|
2012-10-06 06:21:57 +00:00
|
|
|
|
2008-09-02 09:04:54 +00: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 10:32:08 +00:00
|
|
|
def __str__(self):
|
2008-09-02 09:04:54 +00:00
|
|
|
return self.data
|
|
|
|
|
2012-10-06 06:21:57 +00:00
|
|
|
|
2008-09-02 09:04:54 +00:00
|
|
|
class Child5(AbstractBase3):
|
|
|
|
name = models.CharField(max_length=25)
|
|
|
|
|
|
|
|
default = OnlyFred()
|
|
|
|
objects = models.Manager()
|
|
|
|
|
2012-08-12 10:32:08 +00:00
|
|
|
def __str__(self):
|
2008-09-02 09:04:54 +00:00
|
|
|
return self.name
|
|
|
|
|
2012-10-06 06:21:57 +00:00
|
|
|
|
2008-09-02 09:04:54 +00:00
|
|
|
class Child6(Child4):
|
|
|
|
value = models.IntegerField()
|
|
|
|
|
2012-10-06 06:21:57 +00:00
|
|
|
|
2008-09-02 09:04:54 +00:00
|
|
|
class Child7(Parent):
|
2016-02-18 19:27:55 +00:00
|
|
|
objects = models.Manager()
|
2013-09-30 03:21:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
# RelatedManagers
|
|
|
|
class RelatedModel(models.Model):
|
2022-02-03 19:24:19 +00:00
|
|
|
test_gfk = GenericRelation(
|
|
|
|
"RelationModel", content_type_field="gfk_ctype", object_id_field="gfk_id"
|
|
|
|
)
|
2017-05-06 14:56:28 +00:00
|
|
|
exact = models.BooleanField(null=True)
|
2013-09-30 03:21:21 +00:00
|
|
|
|
|
|
|
def __str__(self):
|
2017-01-24 11:22:42 +00:00
|
|
|
return str(self.pk)
|
2013-09-30 03:21:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
class RelationModel(models.Model):
|
2022-02-03 19:24:19 +00:00
|
|
|
fk = models.ForeignKey(RelatedModel, models.CASCADE, related_name="test_fk")
|
2013-09-30 03:21:21 +00:00
|
|
|
|
2022-02-03 19:24:19 +00:00
|
|
|
m2m = models.ManyToManyField(RelatedModel, related_name="test_m2m")
|
2013-09-30 03:21:21 +00:00
|
|
|
|
2015-07-22 14:43:21 +00:00
|
|
|
gfk_ctype = models.ForeignKey(ContentType, models.SET_NULL, null=True)
|
2015-01-22 06:43:49 +00:00
|
|
|
gfk_id = models.IntegerField(null=True)
|
2022-02-03 19:24:19 +00:00
|
|
|
gfk = GenericForeignKey(ct_field="gfk_ctype", fk_field="gfk_id")
|
2013-09-30 03:21:21 +00:00
|
|
|
|
|
|
|
def __str__(self):
|
2017-01-24 11:22:42 +00:00
|
|
|
return str(self.pk)
|