r***@gmail.com
2018-07-19 10:05:37 UTC
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
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.
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.