Discussion:
Serializer's optimization
r***@gmail.com
2018-07-19 10:05:37 UTC
Permalink
Hello, can you help me with next problem:
I have next structure:


#models.py

Foo(BaseModel):
....

Bar(BaseModel):
id = models.UUIDField()
foo = models.ForeignKey(Foo)

Baz(BaseModel):
bar = models.ForeignKey(Bar, related_name='bazes')


Qux(BaseModel):
baz = models.ForeignKey(Baz, related_name='quxes')
q_type = models.IntegerField() # choiceField from ((0, daily), (1,
nightly))

class Meta:
unique_together = ('baz', 'q_type')

Mos(BaseModel):
qux = models.ForeignKey(Qux, related_name='moses')


# serialziers.py
class BazSerializer(serializers.ModelSerializer):
daily = serializers.SerizlierMethodField()
nightly = serializers.SerizlierMethodField()

class Meta:
model = Baz
fields = (..., 'daily', 'nightly')

def get_daily(self, instance):
try:
quxes = instance.quxes.filter(q_type=0).last().moses.all()
except:
return None
return QuxSerializer(quxes, many=True).data

class BarSerializer(serializers.ModelSerializer):
bazes = serializers.SerizlierMethodField()

class Meta:
model = Bar
fields = ('id', 'bazes')

def get_bazes(self, instance):
qs = instance.bazes

return BazSerializer(qs, many=True).data


# views.py
class BarListView(ListAPIView):
serializer_class = BarSerializer

def get_queryset(self):
foo_id = self.kwargs.get('foo_id')
qs = Bar.objects.filter(foo = foo_id)

return qs


When tried to prefetch data like that:

def get_queryset(self):
foo_id = self.kwargs.get('foo_id')
select_related = ['foo']
prefetch_related = ['bazes', 'bazes__quxes', 'bazes__quxes__moses']
qs = Bar.objects.select_related(*select_related).prefetch_related(*
prefetch_related).filter(foo = foo_id)

return qs


But how i can use this prefetched data in serializer, cause every

quxes = instance.quxes.filter(q_type=0).last().moses.all()
*in BazSerializer*

is one more query to database

Thanks, if you understood my question
--
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+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Oleg Nykolyn
2018-07-19 10:29:28 UTC
Permalink
Hi,

Rewriting filter() and last() as operations manual over list/iterator in
"quxes = instance.quxes.filter(q_type=0).last().moses.all()" should
probably help.
Something like:
quxes = [q for q in instance.quxes.all() if q.type==0][-1].moses.all()
This should avoid extra DB queries.

---
Best regards,
Oleg
--
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+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Continue reading on narkive:
Search results for 'Serializer's optimization' (Questions and Answers)
5
replies
can i get question answer of asp.net ?
started 2006-10-11 00:02:47 UTC
software
Loading...