2008-09-02 09:04:54 +00:00
|
|
|
"""
|
|
|
|
Various edge-cases for model managers.
|
|
|
|
"""
|
|
|
|
|
2014-01-22 06:43:33 +00:00
|
|
|
from django.contrib.contenttypes.fields import (
|
2015-01-28 12:35:27 +00:00
|
|
|
GenericForeignKey, GenericRelation,
|
2014-01-22 06:43:33 +00:00
|
|
|
)
|
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
|
2015-01-28 12:35:27 +00:00
|
|
|
from django.utils.encoding import force_text, python_2_unicode_compatible
|
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):
|
|
|
|
return super(OnlyFred, self).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):
|
|
|
|
return super(OnlyBarney, self).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):
|
|
|
|
return super(Value42, self).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
|
|
|
|
2012-08-12 10:32:08 +00:00
|
|
|
@python_2_unicode_compatible
|
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.
|
2012-08-12 10:32:08 +00:00
|
|
|
@python_2_unicode_compatible
|
2008-09-02 09:04:54 +00:00
|
|
|
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
|
|
|
|
2012-08-12 10:32:08 +00:00
|
|
|
@python_2_unicode_compatible
|
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
|
|
|
|
2012-08-12 10:32:08 +00:00
|
|
|
@python_2_unicode_compatible
|
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
|
|
|
|
2012-08-12 10:32:08 +00:00
|
|
|
@python_2_unicode_compatible
|
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
|
|
|
|
2012-08-12 10:32:08 +00:00
|
|
|
@python_2_unicode_compatible
|
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()
|
|
|
|
|
2016-04-17 11:55:55 +00:00
|
|
|
class Meta:
|
|
|
|
manager_inheritance_from_future = True
|
|
|
|
|
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
|
|
|
|
@python_2_unicode_compatible
|
|
|
|
class RelatedModel(models.Model):
|
2014-01-22 06:43:33 +00:00
|
|
|
test_gfk = GenericRelation('RelationModel', content_type_field='gfk_ctype', object_id_field='gfk_id')
|
2015-01-22 06:43:49 +00:00
|
|
|
exact = models.NullBooleanField()
|
2013-09-30 03:21:21 +00:00
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return force_text(self.pk)
|
|
|
|
|
|
|
|
|
|
|
|
@python_2_unicode_compatible
|
|
|
|
class RelationModel(models.Model):
|
2015-07-22 14:43:21 +00:00
|
|
|
fk = models.ForeignKey(RelatedModel, models.CASCADE, related_name='test_fk')
|
2013-09-30 03:21:21 +00:00
|
|
|
|
|
|
|
m2m = models.ManyToManyField(RelatedModel, related_name='test_m2m')
|
|
|
|
|
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)
|
2014-01-22 06:43:33 +00:00
|
|
|
gfk = GenericForeignKey(ct_field='gfk_ctype', fk_field='gfk_id')
|
2013-09-30 03:21:21 +00:00
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return force_text(self.pk)
|