Loading django/db/models/query.py +15 −5 Original line number Diff line number Diff line Loading @@ -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 Loading Loading @@ -1912,8 +1912,18 @@ 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(): 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__)) Loading tests/prefetch_related/tests.py +12 −1 Original line number Diff line number Diff line Loading @@ -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): Loading Loading
django/db/models/query.py +15 −5 Original line number Diff line number Diff line Loading @@ -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 Loading Loading @@ -1912,8 +1912,18 @@ 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(): 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__)) Loading
tests/prefetch_related/tests.py +12 −1 Original line number Diff line number Diff line Loading @@ -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): Loading