How to combine multiple models into a single serializer?

Hello, needed to combine several models(4) one serializer, but there were problems with the implementation.

urls.py
from django.urls import path
from .views import FiltersView

mapping includes = [
 path('filters/' FiltersView.as_view(), name='Filters')
]


views.py
from rest_framework import views
from rest_framework.response import Response
from rest_framework.status import HTTP_200_OK
from .serializers import FiltersSerializers

class FiltersView(views.APIView):
 def get(self, request, *args, **kwargs):
 filters = {}
 filters['model_1'] = Model1.objects.all()
 filters['model_2'] = Model2.objects.all()
 filters['model_3'] = Model3.objects.all()
 serializer = FiltersSerializers(filters, many=True)
 return Response (serializer.data, status=HTTP_200_OK)


serializers.py
from rest_framework import serializers

class FiltersSerializers(serializers.Serializer):
 model_1 = Model1Serializers(read_only=True, many=True)
 model_2 = Model2Serializers(read_only=True)
 model_3 = Model3Serializers(read_only=True)


But the output I get:
HTTP 200 OK
Allow: GET, HEAD, OPTIONS
Content-Type: application/json
Vary: Accept

[
{},
{},
{}
]


What could be the problem?
April 7th 20 at 10:51
1 answer
April 7th 20 at 10:53
Solution
The answer to my question was extremely simple, it turns out I just left the argument many=true in FiltersSerializers.
Correct version looks like this:
class FiltersView(views.APIView):
 def get(self, request, *args, **kwargs):
 filters = {}
 filters['model_1'] = Model1.objects.all()
 filters['model_2'] = Model2.objects.all()
 filters['model_3'] = Model3.objects.all()
 serializer = FiltersSerializers(filters)
 return Response (serializer.data, status=HTTP_200_OK)

Find more questions by tags DjangoDjango Rest Framework