Как объединить много моделей в одном наборе запросов, чтобы можно было использовать HyperlinkedModelSerializer, используя нумерацию страниц? Мне нужно подключить много моделей с одинаковыми полями. Это то, что я сделал, но меня это не устраивает.:
#models.py
class Merketing(models.Model):
question = models.CharField(max_length=500, unique=True)
answer = models.CharField(max_length=500)
class Management(models.Model):
question = models.CharField(max_length=500, unique=True)
answer = models.CharField(max_length=500)
#serializers.py
class MerketingSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Merketing
fields = ['id','question', 'answer']
class ManagementSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Management
fields = ['id','question', 'answer']
class FiltersSerializers(serializers.Serializer):
model_1 = MerketingSerializer(read_only=True,many=True)
model_2 = ManagementSerializer(read_only=True,many=True)
#viesw.py
class MerketingViewSet(viewsets.ModelViewSet):
queryset = Merketing.objects.all().order_by('question')
serializer_class = MerketingSerializer
permission_classes = [permissions.IsAuthenticated]
filter_backends = [filters.SearchFilter]
search_fields = ['question']
class ManagementViewSet(viewsets.ModelViewSet):
queryset = Management.objects.all().order_by('question')
serializer_class = ManagementSerializer
permission_classes = [permissions.IsAuthenticated]
filter_backends = [filters.SearchFilter]
search_fields = ['question']
class FiltersView(APIView):
def get(self, request, *args, **kwargs):
filters = {}
filters['model_1'] = Merketing.objects.all().order_by('question')
filters['model_2'] = Management.objects.all().order_by('question')
serializer = FiltersSerializers(filters)
return Response (serializer.data, status=HTTP_200_OK)