[1.7.x] Refs #25693 -- Added a regression test for to_attr
validation on forward m2m.
Backport of cc8c02fa0fa2119704d1c39ca8509850aef84acc from master
This commit is contained in:
parent
3d037b9f68
commit
fd1426570e
@ -10,7 +10,7 @@ from django.conf import settings
|
|||||||
from django.core import exceptions
|
from django.core import exceptions
|
||||||
from django.db import connections, router, transaction, IntegrityError
|
from django.db import connections, router, transaction, IntegrityError
|
||||||
from django.db.models.constants import LOOKUP_SEP
|
from django.db.models.constants import LOOKUP_SEP
|
||||||
from django.db.models.fields import AutoField, Empty
|
from django.db.models.fields import AutoField, Empty, FieldDoesNotExist
|
||||||
from django.db.models.query_utils import (Q, select_related_descend,
|
from django.db.models.query_utils import (Q, select_related_descend,
|
||||||
deferred_class_factory, InvalidQuery)
|
deferred_class_factory, InvalidQuery)
|
||||||
from django.db.models.deletion import Collector
|
from django.db.models.deletion import Collector
|
||||||
@ -1912,10 +1912,20 @@ def prefetch_one_level(instances, prefetcher, lookup, level):
|
|||||||
# We assume that objects retrieved are homogeneous (which is the premise
|
# We assume that objects retrieved are homogeneous (which is the premise
|
||||||
# of prefetch_related), so what applies to first object applies to all.
|
# of prefetch_related), so what applies to first object applies to all.
|
||||||
model = instances[0].__class__
|
model = instances[0].__class__
|
||||||
for related_m2m in model._meta.get_all_related_many_to_many_objects():
|
opts = model._meta
|
||||||
if related_m2m.get_accessor_name() == to_attr:
|
conflicts = False
|
||||||
msg = 'to_attr={} conflicts with a field on the {} model.'
|
try:
|
||||||
raise ValueError(msg.format(to_attr, model.__name__))
|
opts.get_field(to_attr)
|
||||||
|
except FieldDoesNotExist:
|
||||||
|
for related_m2m in opts.get_all_related_many_to_many_objects():
|
||||||
|
if related_m2m.get_accessor_name() == to_attr:
|
||||||
|
conflicts = True
|
||||||
|
break
|
||||||
|
else:
|
||||||
|
conflicts = True
|
||||||
|
if conflicts:
|
||||||
|
msg = 'to_attr={} conflicts with a field on the {} model.'
|
||||||
|
raise ValueError(msg.format(to_attr, model.__name__))
|
||||||
|
|
||||||
for obj in instances:
|
for obj in instances:
|
||||||
instance_attr_val = instance_attr(obj)
|
instance_attr_val = instance_attr(obj)
|
||||||
|
@ -221,7 +221,18 @@ class PrefetchRelatedTests(TestCase):
|
|||||||
self.assertTrue('prefetch_related' in str(cm.exception))
|
self.assertTrue('prefetch_related' in str(cm.exception))
|
||||||
self.assertTrue("name" in str(cm.exception))
|
self.assertTrue("name" in str(cm.exception))
|
||||||
|
|
||||||
def test_m2m_shadow(self):
|
def test_forward_m2m_to_attr_conflict(self):
|
||||||
|
msg = 'to_attr=authors conflicts with a field on the Book model.'
|
||||||
|
authors = Author.objects.all()
|
||||||
|
with self.assertRaisesMessage(ValueError, msg):
|
||||||
|
list(Book.objects.prefetch_related(
|
||||||
|
Prefetch('authors', queryset=authors, to_attr='authors'),
|
||||||
|
))
|
||||||
|
# Without the ValueError, an author was deleted due to the implicit
|
||||||
|
# save of the relation assignment.
|
||||||
|
self.assertEqual(self.book1.authors.count(), 3)
|
||||||
|
|
||||||
|
def test_reverse_m2m_to_attr_conflict(self):
|
||||||
msg = 'to_attr=books conflicts with a field on the Author model.'
|
msg = 'to_attr=books conflicts with a field on the Author model.'
|
||||||
poems = Book.objects.filter(title='Poems')
|
poems = Book.objects.filter(title='Poems')
|
||||||
with self.assertRaisesMessage(ValueError, msg):
|
with self.assertRaisesMessage(ValueError, msg):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user