Home:ALL Converter>use django-filter with object url_params

use django-filter with object url_params

Ask Time:2018-09-15T11:23:38         Author:ChampR

Json Formatter

I have successfully implemented django-filter on my Django-rest-framework server.

I have the following filter_class

filters.py

class EmploymentFilter(filters.FilterSet):
    class Meta:
        model = EmploymentCheck
        fields  = ['instructions',]

views.py

class EmploymentCheckViewSet(viewsets.ModelViewSet):
    pagination_class = ContentRangeHeaderPagination
    serializer_class = EmploymentCheckSerializer
    queryset = EmploymentCheck.objects.all()
    filter_class = EmploymentFilter

the filter works when i send a get request as

/employmentcheck/?instructions=2

However, I have implemented a front end with react-admin. My front-end, sends a request with the url_params as objects

/employmentcheck/?filter={"instruction_id":"2"}&range=[0,24]&sort=["id","DESC"]/

Notice how the URL specifies a filter object, in which, it defines the parameters to filter against.

My question is, how and where can I filter my model without changing the URL pattern from my client? Any other advise that spans the scope of my question is equally welcomed

Models.py

class EmploymentCheck(models.Model):
    instructions = models.ForeignKey(Instruction, on_delete=models.CASCADE, null=True)

Author:ChampR,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/52341295/use-django-filter-with-object-url-params
yy