1
0
mirror of https://github.com/django/django.git synced 2025-10-23 21:59:11 +00:00

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.
This commit is contained in:
Tim Graham
2017-08-31 09:34:44 -04:00
committed by GitHub
parent ec6481246a
commit e5bd585c6e
6 changed files with 41 additions and 18 deletions

View File

@@ -1,21 +1,13 @@
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), [])
def test_abstract_model_pending_operations(self):
"""
Many-to-many fields declared on abstract models should not add lazy
@@ -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])