summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSimon Charette <charette.s@gmail.com>2015-11-11 00:49:35 -0500
committerSimon Charette <charette.s@gmail.com>2015-11-11 12:42:53 -0500
commitfd1426570e7601128745d0e524704412c4431749 (patch)
treede8ed18674df9072026d529f59dea6aaebb59274
parent3d037b9f689d287567d5961939817d96907a1459 (diff)
downloaddjango-fd1426570e7601128745d0e524704412c4431749.tar.gz
[1.7.x] Refs #25693 -- Added a regression test for `to_attr` validation on forward m2m.
Backport of cc8c02fa0fa2119704d1c39ca8509850aef84acc from master
-rw-r--r--django/db/models/query.py20
-rw-r--r--tests/prefetch_related/tests.py13
2 files changed, 27 insertions, 6 deletions
diff --git a/django/db/models/query.py b/django/db/models/query.py
index ac68bec0dd..a81ee569f6 100644
--- a/django/db/models/query.py
+++ b/django/db/models/query.py
@@ -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)
diff --git a/tests/prefetch_related/tests.py b/tests/prefetch_related/tests.py
index 83db7eb2ff..86240f64ad 100644
--- a/tests/prefetch_related/tests.py
+++ b/tests/prefetch_related/tests.py
@@ -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):