Hi (excuse me for my bad english) !
When I make this:
gallery_qs = Gallery.objects.all()\
.annotate(Count('photos'))\
.extra(select={'photo_id': 'photologue_photo.id'})
The sql query is :
SELECT (photologue_photo.id) AS `photo`, `photologue_gallery`.*
FROM `photologue_gallery`
LEFT OUTER JOIN `photologue_gallery_photos`
ON (`photologue_gallery`.`id` = `photologue_gallery_photos`.`gallery_id`)
LEFT OUTER JOIN `photologue_photo`
ON (`photologue_gallery_photos`.`photo_id` = `photologue_photo`.`id`)
GROUP BY `photologue_gallery`.`id`, photologue_photo.id
ORDER BY `photologue_gallery`.`publication_date` DESC
The problem is that extra
method automatically adds photologue_photo.id in GROUP BY clause. And I need remove it, because it duplicates galleries, for example :
[<Gallery: Lorem ipsum dolor sit amet>, <Gallery: Lorem ipsum dolor sit amet>, <Gallery: Lorem ipsum dolor sit amet>, <Gallery: Lorem ipsum dolor sit amet>, <Gallery: Lorem ipsum dolor sit amet>, <Gallery: Lorem ipsum dolor sit amet>, <Gallery: Lorem ipsum dolor sit amet>]
Si I need to make this query with django, is it possible ?
SELECT (photologue_photo.id) AS `photo`, `photologue_gallery`.*
FROM `photologue_gallery`
LEFT OUTER JOIN `photologue_gallery_photos`
ON (`photologue_gallery`.`id` = `photologue_gallery_photos`.`gallery_id`)
LEFT OUTER JOIN `photologue_photo`
ON (`photologue_gallery_photos`.`photo_id` = `photologue_photo`.`id`)
GROUP BY `photologue_gallery`
ORDER BY `photologue_gallery`.`publication_date` DESC
Thank you ! :)