8๐
โ
Your problem comes from the queryset, not the serializer. By default Django querysets do not follow relations and so the related fields are not populated.
In order to fix that you have to change your queryset to use select_related
for OneToOne
and ForeignKey
fields and prefetch_related
for ManyToMany
fields.
So lets say you are using DRF ViewSets
, you can change your get_queryset
method in the OrderViewSet
to something like that:
def get_queryset(self):
return Order.objects.select_related(
'bought_by', 'category', 'article', 'supplier',
'payment_method', 'delivery_received_by'
).prefetch_related(
'tags', 'invoice_documents'
)
๐คTodor
Source:stackexchange.com