2013-07-30 01:19:04 +08:00
|
|
|
from __future__ import unicode_literals
|
2011-10-14 05:34:56 +08:00
|
|
|
|
2012-10-06 14:21:57 +08:00
|
|
|
from django.db import models
|
2013-09-30 11:21:21 +08:00
|
|
|
from django.template import Context, Template
|
2013-12-23 23:01:13 +08:00
|
|
|
from django.test import TestCase, override_settings
|
2015-11-17 13:39:28 +08:00
|
|
|
from django.test.utils import isolate_apps
|
2013-09-30 11:21:21 +08:00
|
|
|
from django.utils.encoding import force_text
|
2010-10-14 09:17:14 +08:00
|
|
|
|
2012-10-06 14:21:57 +08:00
|
|
|
from .models import (
|
2015-01-28 20:35:27 +08:00
|
|
|
AbstractBase1, AbstractBase2, AbstractBase3, Child1, Child2, Child3,
|
|
|
|
Child4, Child5, Child6, Child7, RelatedModel, RelationModel,
|
2012-10-06 14:21:57 +08:00
|
|
|
)
|
2010-10-14 09:17:14 +08:00
|
|
|
|
|
|
|
|
|
|
|
class ManagersRegressionTests(TestCase):
|
|
|
|
def test_managers(self):
|
2012-10-06 14:21:57 +08:00
|
|
|
Child1.objects.create(name='fred', data='a1')
|
|
|
|
Child1.objects.create(name='barney', data='a2')
|
|
|
|
Child2.objects.create(name='fred', data='b1', value=1)
|
|
|
|
Child2.objects.create(name='barney', data='b2', value=42)
|
|
|
|
Child3.objects.create(name='fred', data='c1', comment='yes')
|
|
|
|
Child3.objects.create(name='barney', data='c2', comment='no')
|
|
|
|
Child4.objects.create(name='fred', data='d1')
|
|
|
|
Child4.objects.create(name='barney', data='d2')
|
|
|
|
Child5.objects.create(name='fred', comment='yes')
|
|
|
|
Child5.objects.create(name='barney', comment='no')
|
|
|
|
Child6.objects.create(name='fred', data='f1', value=42)
|
|
|
|
Child6.objects.create(name='barney', data='f2', value=42)
|
|
|
|
Child7.objects.create(name='fred')
|
|
|
|
Child7.objects.create(name='barney')
|
2010-10-14 09:17:14 +08:00
|
|
|
|
|
|
|
self.assertQuerysetEqual(Child1.manager1.all(), ["<Child1: a1>"])
|
|
|
|
self.assertQuerysetEqual(Child1.manager2.all(), ["<Child1: a2>"])
|
|
|
|
self.assertQuerysetEqual(Child1._default_manager.all(), ["<Child1: a1>"])
|
|
|
|
|
|
|
|
self.assertQuerysetEqual(Child2._default_manager.all(), ["<Child2: b1>"])
|
|
|
|
self.assertQuerysetEqual(Child2.restricted.all(), ["<Child2: b2>"])
|
|
|
|
|
|
|
|
self.assertQuerysetEqual(Child3._default_manager.all(), ["<Child3: c1>"])
|
|
|
|
self.assertQuerysetEqual(Child3.manager1.all(), ["<Child3: c1>"])
|
|
|
|
self.assertQuerysetEqual(Child3.manager2.all(), ["<Child3: c2>"])
|
|
|
|
|
|
|
|
# Since Child6 inherits from Child4, the corresponding rows from f1 and
|
|
|
|
# f2 also appear here. This is the expected result.
|
|
|
|
self.assertQuerysetEqual(Child4._default_manager.order_by('data'), [
|
2013-10-20 07:33:10 +08:00
|
|
|
"<Child4: d1>",
|
|
|
|
"<Child4: d2>",
|
|
|
|
"<Child4: f1>",
|
|
|
|
"<Child4: f2>"
|
2013-10-18 17:02:43 +08:00
|
|
|
]
|
2010-10-14 09:17:14 +08:00
|
|
|
)
|
|
|
|
self.assertQuerysetEqual(Child4.manager1.all(), [
|
2013-10-20 07:33:10 +08:00
|
|
|
"<Child4: d1>",
|
|
|
|
"<Child4: f1>"
|
2013-10-18 17:02:43 +08:00
|
|
|
],
|
2012-12-13 19:33:11 +08:00
|
|
|
ordered=False
|
2010-10-14 09:17:14 +08:00
|
|
|
)
|
|
|
|
self.assertQuerysetEqual(Child5._default_manager.all(), ["<Child5: fred>"])
|
|
|
|
self.assertQuerysetEqual(Child6._default_manager.all(), ["<Child6: f1>"])
|
|
|
|
self.assertQuerysetEqual(Child7._default_manager.order_by('name'), [
|
2013-10-20 07:33:10 +08:00
|
|
|
"<Child7: barney>",
|
|
|
|
"<Child7: fred>"
|
2013-10-18 17:02:43 +08:00
|
|
|
]
|
2010-10-14 09:17:14 +08:00
|
|
|
)
|
2012-10-06 14:21:57 +08:00
|
|
|
|
|
|
|
def test_abstract_manager(self):
|
|
|
|
# Accessing the manager on an abstract model should
|
|
|
|
# raise an attribute error with an appropriate message.
|
2015-07-03 02:23:28 +08:00
|
|
|
# This error message isn't ideal, but if the model is abstract and
|
|
|
|
# a lot of the class instantiation logic isn't invoked; if the
|
|
|
|
# manager is implied, then we don't get a hook to install the
|
|
|
|
# error-raising manager.
|
|
|
|
msg = "type object 'AbstractBase3' has no attribute 'objects'"
|
|
|
|
with self.assertRaisesMessage(AttributeError, msg):
|
2012-10-06 14:21:57 +08:00
|
|
|
AbstractBase3.objects.all()
|
|
|
|
|
|
|
|
def test_custom_abstract_manager(self):
|
|
|
|
# Accessing the manager on an abstract model with an custom
|
|
|
|
# manager should raise an attribute error with an appropriate
|
|
|
|
# message.
|
2015-07-03 02:23:28 +08:00
|
|
|
msg = "Manager isn't available; AbstractBase2 is abstract"
|
|
|
|
with self.assertRaisesMessage(AttributeError, msg):
|
2012-10-06 14:21:57 +08:00
|
|
|
AbstractBase2.restricted.all()
|
|
|
|
|
|
|
|
def test_explicit_abstract_manager(self):
|
|
|
|
# Accessing the manager on an abstract model with an explicit
|
|
|
|
# manager should raise an attribute error with an appropriate
|
|
|
|
# message.
|
2015-07-03 02:23:28 +08:00
|
|
|
msg = "Manager isn't available; AbstractBase1 is abstract"
|
|
|
|
with self.assertRaisesMessage(AttributeError, msg):
|
2012-10-06 14:21:57 +08:00
|
|
|
AbstractBase1.objects.all()
|
|
|
|
|
2013-10-14 21:14:17 +08:00
|
|
|
@override_settings(TEST_SWAPPABLE_MODEL='managers_regress.Parent')
|
2015-11-17 13:39:28 +08:00
|
|
|
@isolate_apps('managers_regress')
|
2012-10-06 14:21:57 +08:00
|
|
|
def test_swappable_manager(self):
|
2015-11-17 13:39:28 +08:00
|
|
|
class SwappableModel(models.Model):
|
|
|
|
class Meta:
|
|
|
|
swappable = 'TEST_SWAPPABLE_MODEL'
|
|
|
|
|
|
|
|
# Accessing the manager on a swappable model should
|
|
|
|
# raise an attribute error with a helpful message
|
|
|
|
msg = (
|
|
|
|
"Manager isn't available; 'managers_regress.SwappableModel' "
|
|
|
|
"has been swapped for 'managers_regress.Parent'"
|
|
|
|
)
|
|
|
|
with self.assertRaisesMessage(AttributeError, msg):
|
|
|
|
SwappableModel.objects.all()
|
2012-10-06 14:21:57 +08:00
|
|
|
|
2013-10-14 21:14:17 +08:00
|
|
|
@override_settings(TEST_SWAPPABLE_MODEL='managers_regress.Parent')
|
2015-11-17 13:39:28 +08:00
|
|
|
@isolate_apps('managers_regress')
|
2012-10-06 14:21:57 +08:00
|
|
|
def test_custom_swappable_manager(self):
|
2015-11-17 13:39:28 +08:00
|
|
|
class SwappableModel(models.Model):
|
|
|
|
stuff = models.Manager()
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
swappable = 'TEST_SWAPPABLE_MODEL'
|
|
|
|
|
|
|
|
# Accessing the manager on a swappable model with an
|
|
|
|
# explicit manager should raise an attribute error with a
|
|
|
|
# helpful message
|
|
|
|
msg = (
|
|
|
|
"Manager isn't available; 'managers_regress.SwappableModel' "
|
|
|
|
"has been swapped for 'managers_regress.Parent'"
|
|
|
|
)
|
|
|
|
with self.assertRaisesMessage(AttributeError, msg):
|
|
|
|
SwappableModel.stuff.all()
|
2012-10-06 14:21:57 +08:00
|
|
|
|
2013-10-14 21:14:17 +08:00
|
|
|
@override_settings(TEST_SWAPPABLE_MODEL='managers_regress.Parent')
|
2015-11-17 13:39:28 +08:00
|
|
|
@isolate_apps('managers_regress')
|
2012-10-06 14:21:57 +08:00
|
|
|
def test_explicit_swappable_manager(self):
|
2015-11-17 13:39:28 +08:00
|
|
|
class SwappableModel(models.Model):
|
|
|
|
objects = models.Manager()
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
swappable = 'TEST_SWAPPABLE_MODEL'
|
|
|
|
|
|
|
|
# Accessing the manager on a swappable model with an
|
|
|
|
# explicit manager should raise an attribute error with a
|
|
|
|
# helpful message
|
|
|
|
msg = (
|
|
|
|
"Manager isn't available; 'managers_regress.SwappableModel' "
|
|
|
|
"has been swapped for 'managers_regress.Parent'"
|
|
|
|
)
|
|
|
|
with self.assertRaisesMessage(AttributeError, msg):
|
|
|
|
SwappableModel.objects.all()
|
2013-09-30 11:21:21 +08:00
|
|
|
|
|
|
|
def test_regress_3871(self):
|
|
|
|
related = RelatedModel.objects.create()
|
|
|
|
|
|
|
|
relation = RelationModel()
|
|
|
|
relation.fk = related
|
|
|
|
relation.gfk = related
|
|
|
|
relation.save()
|
|
|
|
relation.m2m.add(related)
|
|
|
|
|
|
|
|
t = Template('{{ related.test_fk.all.0 }}{{ related.test_gfk.all.0 }}{{ related.test_m2m.all.0 }}')
|
|
|
|
|
|
|
|
self.assertEqual(
|
|
|
|
t.render(Context({'related': related})),
|
|
|
|
''.join([force_text(relation.pk)] * 3),
|
|
|
|
)
|
2015-01-22 14:43:49 +08:00
|
|
|
|
|
|
|
def test_field_can_be_called_exact(self):
|
|
|
|
# Make sure related managers core filters don't include an
|
|
|
|
# explicit `__exact` lookup that could be interpreted as a
|
|
|
|
# reference to a foreign `exact` field. refs #23940.
|
|
|
|
related = RelatedModel.objects.create(exact=False)
|
|
|
|
relation = related.test_fk.create()
|
|
|
|
self.assertEqual(related.test_fk.get(), relation)
|