Fixed #23001 -- Fixed mixing defer and annotations

This commit is contained in:
Josh Smeaton 2014-07-11 16:55:49 +10:00 committed by Anssi Kääriäinen
parent 954e7b8aad
commit 5b0375ec3e
2 changed files with 26 additions and 1 deletions

View File

@ -260,7 +260,7 @@ class QuerySet(object):
load_fields.append(field.name) load_fields.append(field.name)
skip = None skip = None
if load_fields and not fill_cache: if load_fields:
# Some fields have been deferred, so we have to initialize # Some fields have been deferred, so we have to initialize
# via keyword arguments. # via keyword arguments.
skip = set() skip = set()

View File

@ -88,6 +88,31 @@ class BaseAggregateTestCase(TestCase):
) )
self.assertEqual(b.mean_age, 34.5) self.assertEqual(b.mean_age, 34.5)
def test_annotate_defer(self):
qs = Book.objects.annotate(
page_sum=Sum("pages")).defer('name').filter(pk=1)
rows = [
(1, "159059725", 447, "The Definitive Guide to Django: Web Development Done Right")
]
self.assertQuerysetEqual(
qs.order_by('pk'), rows,
lambda r: (r.id, r.isbn, r.page_sum, r.name)
)
def test_annotate_defer_select_related(self):
qs = Book.objects.select_related('contact').annotate(
page_sum=Sum("pages")).defer('name').filter(pk=1)
rows = [
(1, "159059725", 447, "Adrian Holovaty",
"The Definitive Guide to Django: Web Development Done Right")
]
self.assertQuerysetEqual(
qs.order_by('pk'), rows,
lambda r: (r.id, r.isbn, r.page_sum, r.contact.name, r.name)
)
def test_annotate_m2m(self): def test_annotate_m2m(self):
books = Book.objects.filter(rating__lt=4.5).annotate(Avg("authors__age")).order_by("name") books = Book.objects.filter(rating__lt=4.5).annotate(Avg("authors__age")).order_by("name")
self.assertQuerysetEqual( self.assertQuerysetEqual(