Answer the question
In order to leave comments, you need to log in
Why doesn't filtering work in ModelViewSet in DRF?
As I understand it, in order to be able to work with the database from REST requests, there is an excellent ModelViewSet model and it contains filter_backends and filter_fields, which describes the fields by which you can filter the request to the database.
The code is made following the example from the docs and docs , but it throws an error:
AttributeError 'QuerySet' object has no attribute 'model' on a GET request.
I could not determine what was wrong, I reviewed a bunch of examples and it works for everyone as described in the documentation. There was an idea that perhaps the problem is in some imports. Since I'm working with mongoengine , some imports are changed from rest_framework to rest_framework_mongoengine. But I saw examples with these imports, where supposedly everything works. What's the matter?
The code itself:
1) Serializer
from rest_framework_mongoengine.serializers import DocumentSerializer
from processing.models import SomeModel
class MySerializer(DocumentSerializer):
class Meta:
model = SomeModel
fields = "__all__"
from rest_framework_mongoengine.viewsets import ModelViewSet as MongoModelViewSet
from django_filters.rest_framework import DjangoFilterBackend
from api.v4.serializers import MySerializer
from processing.models import SomeModel
class UsersViewSet(MongoModelViewSet):
# queryset = SomeModel.objects.all()
serializer_class = MySerializer
filter_backends = (DjangoFilterBackend,)
filter_fields = ('user', 'user_code') # все как в документации
def get_queryset(self):
return SomeModel.objects.all()
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question