Multiple models seaarch with Django Rest Framework

Viewed 25

I need to make a search through some different models and than make an endpoint for it with DRF.

So my attempt to make it looks like this

class DataSearch(FlatMultipleModelAPIView):
    def get_querylist(self):
        q = self.request.query_params.get('query')
        
        if q:
            vector_data = SearchVector('research', 'data', 'research__name')
            vector_var = SearchVector('label', )
            query = SearchQuery(q, search_type='websearch')
            querylist = [
                    {
                        'queryset': Data.objects.annotate(search=vector_data).filter(search=query),
                        'serializer_class': DataSerializer
                    },
                    {
                        'queryset': Variable.objects.annotate(search=vector_var).filter(search=query),
                        'serializer_class': VariableSerializer
                    }
            ]
        else:
            querylist = [Data.objects.none()]
        
        return querylist

Here I'm using DjangoRestMultipleModels for making query goes by two models.

When I'm trying to run thin stuff I have an error

DataSearch cannot use the regular Rest Framework or Django paginators as is. Use one of the included paginators from drf_multiple_models.pagination or subclass a paginator to add the format_response` method.

Any ideas of what paginators doing here? Or how to make all this stuff work correctly?

0 Answers
Related