summaryrefslogtreecommitdiff
path: root/tests/model_fields
diff options
context:
space:
mode:
authorTim Graham <timograham@gmail.com>2017-08-31 09:34:44 -0400
committerGitHub <noreply@github.com>2017-08-31 09:34:44 -0400
commite5bd585c6eb1e13e2f8aac030b33c077b0b70c05 (patch)
treec4ae599950d749a1b67f1b9273bd7458f8fc3e0e /tests/model_fields
parentec6481246a76f6a3339c987f14c3878f55cd7300 (diff)
Fixed #28543 -- Prevented ManyToManyField.value_from_object() from being lazy.
Previously, it was a QuerySet which could reevaluate to a new value if the model's data changes. This is inconsistent with other Field.value_from_object() methods. This allows reverting the fix in the admin for refs #27998.
Diffstat (limited to 'tests/model_fields')
-rw-r--r--tests/model_fields/models.py4
-rw-r--r--tests/model_fields/test_manytomanyfield.py27
2 files changed, 20 insertions, 11 deletions
diff --git a/tests/model_fields/models.py b/tests/model_fields/models.py
index 2208a8d4b8..1d18e78869 100644
--- a/tests/model_fields/models.py
+++ b/tests/model_fields/models.py
@@ -360,6 +360,10 @@ class AllFieldsModel(models.Model):
gr = GenericRelation(DataModel)
+class ManyToMany(models.Model):
+ m2m = models.ManyToManyField('self')
+
+
###############################################################################
diff --git a/tests/model_fields/test_manytomanyfield.py b/tests/model_fields/test_manytomanyfield.py
index b270048bd8..5724fe9384 100644
--- a/tests/model_fields/test_manytomanyfield.py
+++ b/tests/model_fields/test_manytomanyfield.py
@@ -1,20 +1,12 @@
from django.apps import apps
from django.db import models
-from django.test import SimpleTestCase
+from django.test import SimpleTestCase, TestCase
from django.test.utils import isolate_apps
+from .models import ManyToMany
-class ManyToManyFieldTests(SimpleTestCase):
-
- @isolate_apps('model_fields')
- def test_value_from_object_instance_without_pk(self):
- class ManyToManyModel(models.Model):
- m2m = models.ManyToManyField('self', models.CASCADE)
- instance = ManyToManyModel()
- qs = instance._meta.get_field('m2m').value_from_object(instance)
- self.assertEqual(qs.model, ManyToManyModel)
- self.assertEqual(list(qs), [])
+class ManyToManyFieldTests(SimpleTestCase):
def test_abstract_model_pending_operations(self):
"""
@@ -66,3 +58,16 @@ class ManyToManyFieldTests(SimpleTestCase):
assert_app_model_resolved('model_fields')
assert_app_model_resolved('tests')
+
+
+class ManyToManyFieldDBTests(TestCase):
+
+ def test_value_from_object_instance_without_pk(self):
+ obj = ManyToMany()
+ self.assertEqual(obj._meta.get_field('m2m').value_from_object(obj), [])
+
+ def test_value_from_object_instance_with_pk(self):
+ obj = ManyToMany.objects.create()
+ related_obj = ManyToMany.objects.create()
+ obj.m2m.add(related_obj)
+ self.assertEqual(obj._meta.get_field('m2m').value_from_object(obj), [related_obj])