1👍
✅
You can use prefetch_related on reverse relationships, so the above code in queryset() can be replaced with:
paintings = super(PaintingAdmin, self).queryset(request)
return paintings.prefetch_related('images')
The problem was with this method on Painting:
def thumbnail(self):
thumbnail = self.images.filter(position=0)
if thumbnail.exists():
return thumbnail[0].thumbnail_html()
else:
return ''
Even when using prefetch_related, it will do extra queries. So the solution was to replace it with:
def thumbnail(self):
if self.images.count():
return self.images.all()[0].thumbnail_html()
else:
return ''
Source:stackexchange.com