Django 1.11 Annotating a Subquery Aggregate

后端 未结 6 863
醉酒成梦
醉酒成梦 2020-11-28 04:22

This is a bleeding-edge feature that I\'m currently skewered upon and quickly bleeding out. I want to annotate a subquery-aggregate onto an existing queryset. Doing this bef

6条回答
  •  天涯浪人
    2020-11-28 04:41

    "works for me" doesn't help very much. But. I tried your example on some models I had handy (the Book -> Author type), it works fine for me in django 1.11b1.

    Are you sure you're running this in the right version of Django? Is this the actual code you're running? Are you actually testing this not on carpark but some more complex model?

    Maybe try to print(thequery.query) to see what SQL it's trying to run in the database. Below is what I got with my models (edited to fit your question):

    SELECT (SELECT COUNT(U0."id") AS "c"
    FROM "carparks_spaces" U0
    WHERE U0."carpark_id" = ("carparks_carpark"."id")
    GROUP BY U0."carpark_id") AS "space_count" FROM "carparks_carpark"
    

    Not really an answer, but hopefully it helps.

提交回复
热议问题