summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLex Berezhny <lex@damoti.com>2017-01-31 18:45:55 -0500
committerTim Graham <timograham@gmail.com>2017-01-31 18:46:07 -0500
commitd43f8478478c333ed8b7e499abef17a86363d61c (patch)
tree469745e06458ab1727c58985030bd80e14d99067
parent8afe790df91faf45f13bccbb69310b45afcb1b30 (diff)
[1.11.x] Fixed #27800 -- Fixed QuerySet.annotate(Length(...)).distinct() crash.
Backport of ac5f886c5610a6bca26dab10170b445d1e9df450 from master
-rw-r--r--AUTHORS1
-rw-r--r--django/db/models/sql/query.py7
-rw-r--r--tests/annotations/tests.py7
3 files changed, 13 insertions, 2 deletions
diff --git a/AUTHORS b/AUTHORS
index dbc414f5a3..bf7c21be45 100644
--- a/AUTHORS
+++ b/AUTHORS
@@ -456,6 +456,7 @@ answer newbie questions, and generally made Django that much better:
Leo Shklovskii
Leo Soto <leo.soto@gmail.com>
lerouxb@gmail.com
+ Lex Berezhny <lex@damoti.com>
Liang Feng <hutuworm@gmail.com>
limodou
Loek van Gent <loek@barakken.nl>
diff --git a/django/db/models/sql/query.py b/django/db/models/sql/query.py
index 5f65786373..695e52cda7 100644
--- a/django/db/models/sql/query.py
+++ b/django/db/models/sql/query.py
@@ -1345,7 +1345,12 @@ class Query(object):
"querying. If it is a GenericForeignKey, consider "
"adding a GenericRelation." % name
)
- model = field.model._meta.concrete_model
+ try:
+ model = field.model._meta.concrete_model
+ except AttributeError:
+ # QuerySet.annotate() may introduce fields that aren't
+ # attached to a model.
+ model = None
else:
# We didn't find the current field, so move position back
# one step.
diff --git a/tests/annotations/tests.py b/tests/annotations/tests.py
index b90286b9da..36e675402c 100644
--- a/tests/annotations/tests.py
+++ b/tests/annotations/tests.py
@@ -8,7 +8,7 @@ from django.db.models import (
BooleanField, CharField, Count, DateTimeField, ExpressionWrapper, F, Func,
IntegerField, NullBooleanField, Q, Sum, Value,
)
-from django.db.models.functions import Lower
+from django.db.models.functions import Length, Lower
from django.test import TestCase, skipUnlessDBFeature
from django.utils import six
@@ -208,6 +208,11 @@ class NonAggregateAnnotationTestCase(TestCase):
).distinct('test_alias')
self.assertEqual(len(people2), 1)
+ lengths = Employee.objects.annotate(
+ name_len=Length('first_name'),
+ ).distinct('name_len').values_list('name_len', flat=True)
+ self.assertSequenceEqual(lengths, [3, 7, 8])
+
def test_filter_annotation(self):
books = Book.objects.annotate(
is_book=Value(1, output_field=IntegerField())