1
0
mirror of https://github.com/django/django.git synced 2025-10-31 09:41:08 +00:00

Fixed #33772 -- Added QuerySet.first()/last() error message on unordered queryset with aggregation.

This commit is contained in:
Pablo Pissi
2022-06-12 22:08:21 -03:00
committed by Mariusz Felisiak
parent db588d4f0e
commit d287294885
4 changed files with 47 additions and 3 deletions

View File

@@ -16,6 +16,11 @@ class Person(models.Model):
# Note that this model doesn't have "get_latest_by" set.
class Comment(models.Model):
article = models.ForeignKey(Article, on_delete=models.CASCADE)
likes_count = models.PositiveIntegerField()
# Ticket #23555 - model with an intentionally broken QuerySet.__iter__ method.

View File

@@ -1,8 +1,9 @@
from datetime import datetime
from django.db.models import Avg
from django.test import TestCase
from .models import Article, IndexErrorArticle, Person
from .models import Article, Comment, IndexErrorArticle, Person
class EarliestOrLatestTests(TestCase):
@@ -245,3 +246,21 @@ class TestFirstLast(TestCase):
expire_date=datetime(2005, 9, 1),
)
check()
def test_first_last_unordered_qs_aggregation_error(self):
a1 = Article.objects.create(
headline="Article 1",
pub_date=datetime(2005, 7, 26),
expire_date=datetime(2005, 9, 1),
)
Comment.objects.create(article=a1, likes_count=5)
qs = Comment.objects.values("article").annotate(avg_likes=Avg("likes_count"))
msg = (
"Cannot use QuerySet.%s() on an unordered queryset performing aggregation. "
"Add an ordering with order_by()."
)
with self.assertRaisesMessage(TypeError, msg % "first"):
qs.first()
with self.assertRaisesMessage(TypeError, msg % "last"):
qs.last()