Answer the question
In order to leave comments, you need to log in
How to create related queries in django rest?
Hello. Immediately for example.
There is model A and model B.
The client that works with my API wants data from model B, but he does not have any data for this to make a request. Therefore, it goes to model A, receives data, and based on this data, goes to model B and receives data from there.
As a result, two requests. And if there are 5 such models? It is necessary to fulfill five requests at least. Is there any practice for implementing related requests in Django Rest?
Answer the question
In order to leave comments, you need to log in
I suspect you need something like this:
class CompanyContactSerializer(serializers.ModelSerializer):
class Meta:
model = models.CompanyContact
fields = ('id', 'type', 'value')
class CompanyDetailSerializer(serializers.ModelSerializer):
contacts = CompanyContactSerializer(many=True, read_only=True)
class Meta:
model = models.Company
fields = ('id', 'name', 'contacts')
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question