I'm using Django and i have one question about models. I have 2 models, both related to a 'ManyToMany' field.
I want a query that gets the data of all tags, the last image with the tag and the number of images with the tag. I used this code
selargs = (
'id',
'image__title',
'image__smallImage',
'image__smallWidth',
'image__smallHeight',
'name',
'nameCleaned')
tags = Tag.objects.values(*selargs).annotate(
imageCount=Count('image__id')).order_by('-image__date')
The code works but the problem is the sql sentence, is so .... pfff
SELECT "gallery_tag"."id", "gallery_image"."title", "gallery_image"."smallImage", "gallery_image"."smallWidth", "gallery_image"."smallHeight", "gallery_tag"."name", "gallery_tag"."nameCleaned", COUNT("gallery_image_tags"."image_id") AS "imageCount"
FROM "gallery_tag"
LEFT OUTER JOIN "gallery_image_tags" ON ( "gallery_tag"."id" = "gallery_image_tags"."tag_id" )
LEFT OUTER JOIN "gallery_image" ON ( "gallery_image_tags"."image_id" = "gallery_image"."id" )
GROUP BY "gallery_tag"."id", "gallery_image"."title", "gallery_image"."smallImage", "gallery_image"."smallWidth", "gallery_image"."smallHeight", "gallery_tag"."name", "gallery_tag"."nameCleaned", "gallery_image"."date"
ORDER BY "gallery_image"."date"
i don't know if this is effective. Is there a better way? Is important that i get the data in one operation because after i use this query in a paginator for limit the the records. Is possible do this in one query or is it better use more models operations?
Thank you.