Has been solved in SO On Thursday, April 12, 2018 at 9:27:52 AM UTC-5, Jonathan Pham wrote: > > Posted SO question with code examples: > > https://stackoverflow.com/questions/49780731/how-does-drf-serialize-manytomany-fields > > Trying to optimize my serializer. > DRF automatically renders a manytomany as a list of pks. Using the default > with prefetch_related takes only 2 queries. > All I want to do is return an intersection of that list with the queried > pks. > > def get_failbin_regressions(self, obj): > runids = self.context.get('runids') > return obj.runs.values_list('id', flat=True) > > > > However when accessing the many to many in a serializermethodfield causes > a query for each row. Why is this? Is the prefetch lost in a > serializermethodfield? > The end goal is to get a list with the same results as return obj.runs. > values_list('id', flat=True).filter(id__in=runids) without the extra > queries. > > Thanks! >
-- You received this message because you are subscribed to the Google Groups "Django REST framework" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-framework+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.