[1.7.x] Refs #25693 -- Added a regression test for `to_attr` validation on forward m2m.

Backport of cc8c02fa0f from master
This commit is contained in:
Simon Charette 2015-11-11 00:49:35 -05:00
parent 3d037b9f68
commit fd1426570e
2 changed files with 27 additions and 6 deletions

View File

@ -10,7 +10,7 @@ from django.conf import settings
from django.core import exceptions
from django.db import connections, router, transaction, IntegrityError
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,
deferred_class_factory, InvalidQuery)
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
# of prefetch_related), so what applies to first object applies to all.
model = instances[0].__class__
for related_m2m in model._meta.get_all_related_many_to_many_objects():
if related_m2m.get_accessor_name() == to_attr:
msg = 'to_attr={} conflicts with a field on the {} model.'
raise ValueError(msg.format(to_attr, model.__name__))
opts = model._meta
conflicts = False
try:
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:
instance_attr_val = instance_attr(obj)

View File

@ -221,7 +221,18 @@ class PrefetchRelatedTests(TestCase):
self.assertTrue('prefetch_related' 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.'
poems = Book.objects.filter(title='Poems')
with self.assertRaisesMessage(ValueError, msg):