I'm working with the Django REST framework library and I am trying to make a filter that can filter by first_name, last_name, or by both of them. This is my ContactViewSet.py:
class ContactViewSet(viewsets.ModelViewSet):
queryset = Contact.objects.all()
serializer_class = ContactSerializer
filter_backends = (DjangoFilterBackend, )
filter_fields = ('first_name', 'last_name')
lookup_field = 'idContact'
My DRF's settings.py
:
REST_FRAMEWORK = {
'DEFAULT_FILTER_BACKENDS': ('django_filters.rest_framework.DjangoFilterBackend',),
}
My current request URL looks like:
http://localhost:8000/api/v1/contacts/?first_name=Clair&last_name=Test
But I'm looking for something like this:
http://localhost:8000/api/v1/contacts/?first_name=Cl**&last_name=Tes**
I solved my problem by modifying my class ContactFilter like this:
And in my view I just had to do this:
My request URL looks like this:
But I still wonder if I can have something like this in Django: