No annotation attribute in list in modeltests.aggregate

Create issue
Issue #59 wontfix
Johan Harjono created an issue

{{{ #!python

File "/home/johan/workspace/fiji/build/tests/modeltests/aggregation/", line ?, in modeltests.aggregation.models.test.API_TESTS Failed example: list(Book.objects.filter(pk=1).annotate(mean_age=Avg('authors__age')).values()) Expected: [{'rating': 4.5, 'isbn': u'159059725', 'name': u'The Definitive Guide to Django: Web Development Done Right', 'pubdate':, 12, 6), 'price': Decimal("30..."), 'contact_id': 1, 'id': 1, 'publisher_id': 1, 'pages': 447, 'mean_age': 34.5}] Got: []

File "/home/johan/workspace/fiji/build/tests/modeltests/aggregation/", line ?, in modeltests.aggregation.models.test.API_TESTS Failed example: list(Book.objects.filter(pk=1).values()).annotate(mean_age=Avg('authorsage')) Exception raised: Traceback (most recent call last): File "/usr/local/lib/python3.1/dist-packages/django/test/", line 1307, in run compileflags, 1), test.globs) File "<doctest modeltests.aggregation.models.test.API_TESTS[36]>", line 1, in <module> list(Book.objects.filter(pk=1).values()).annotate(mean_age=Avg('authors__age')) AttributeError: 'list' object has no attribute 'annotate' }}}

Comments (5)

  1. Dave Weber
    • changed status to open

    My intuition for a fix at this point is to seperate the SQL query and list conversion. The list argument (SQL query) should be valid, as far as I can tell, so I think storing the result of the query and then converting it to a string will solve this. I don't understand the necessity of converting it to a list in the first place though.

  2. Dave Weber
    • changed status to new

    Python 2 code: Book.objects.filter(pk=1).annotate(mean_age=Avg('authorsage')).values() Python 3 code: list(Book.objects.filter(pk=1).annotate(mean_age=Avg('authorsage')).values())

    Seems like 2to3 will add the list it necessary?

  3. Dave Weber

    Original line:


    After Py2to3 conversion, the line is converted to :


    There is a problem with the conversion - the list() conversion is wrapped around the values call, and splits the query. The aggregate annotate function is then applied to list, which is incorrect. Brackets are needed to wrap around the entire query and force the list() conversion to applied over the entire query.

    To remove the error, the line should be:


    I do not yet know how to force this new line.

  4. Dave Weber

    Emailed Martin van Loewis about possibly solutions, but have not recieved a response, so cannot implement a solution.

  5. Log in to comment