Home:ALL Converter>Filter is not working in Django Rest Framework

Filter is not working in Django Rest Framework

Ask Time:2020-02-24T15:20:09         Author:Loran

Json Formatter

I would like to filter my data like assumes I have one model =>

  • User
    • UserID
    • UserName
    • UserDescription

What I want is=>

  • If i select like => api/user/?userid=1 , It should return only userid == 1 result.
  • If I select like => api/user/?username=test, It should return only username == test result.

What I have done?

I installed django-filter and add this

REST_FRAMEWORK = {
    'DEFAULT_FILTER_BACKENDS': ['django_filters.rest_framework.DjangoFilterBackend']
}

to setting.py and I test like that but

why I get all of my records? even URL is select only userid = 1 api/user/?userid=1

and

If I test with the wrong URL parameter and I got all records even the wrong parameter.

I do the exact same way with this => https://www.django-rest-framework.org/api-guide/filtering/#generic-filtering

Update

Here is View

class LeaveViewSet(viewsets.ModelViewSet):
    queryset = Leave.objects.all()
    serializer_class = LeaveSerializer

Author:Loran,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/60371145/filter-is-not-working-in-django-rest-framework
yy