mirror of
https://github.com/django/django.git
synced 2024-12-23 01:25:58 +00:00
Fixed #29320 -- Added an exception when an annotation alias matches a ForeignKey attname.
This commit is contained in:
parent
78f8b80f9b
commit
e1f13f1551
@ -7,6 +7,7 @@ import operator
|
||||
import warnings
|
||||
from collections import OrderedDict, namedtuple
|
||||
from functools import lru_cache
|
||||
from itertools import chain
|
||||
|
||||
from django.conf import settings
|
||||
from django.core import exceptions
|
||||
@ -981,7 +982,10 @@ class QuerySet:
|
||||
clone = self._chain()
|
||||
names = self._fields
|
||||
if names is None:
|
||||
names = {f.name for f in self.model._meta.get_fields()}
|
||||
names = set(chain.from_iterable(
|
||||
(field.name, field.attname) if hasattr(field, 'attname') else (field.name,)
|
||||
for field in self.model._meta.get_fields()
|
||||
))
|
||||
|
||||
for alias, annotation in annotations.items():
|
||||
if alias in names:
|
||||
|
@ -769,6 +769,11 @@ class AggregationTests(TestCase):
|
||||
with self.assertRaisesMessage(ValueError, msg):
|
||||
Author.objects.annotate(friends=Count('friends'))
|
||||
|
||||
def test_fk_attname_conflict(self):
|
||||
msg = "The annotation 'contact_id' conflicts with a field on the model."
|
||||
with self.assertRaisesMessage(ValueError, msg):
|
||||
Book.objects.annotate(contact_id=F('publisher_id'))
|
||||
|
||||
def test_values_queryset_non_conflict(self):
|
||||
# Regression for #14707 -- If you're using a values query set, some potential conflicts are avoided.
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user