miki725 / django-url-filter

Django URL Filter provides a safe way to filter data via human-friendly URLs.
http://django-url-filter.readthedocs.io/
Other
334 stars 77 forks source link

Question - How to make case insensitive filter? #59

Open rajesh-h opened 6 years ago

rajesh-h commented 6 years ago

Hi,

How can I achieve case insensitive filter on my requests?

for eg: http://127.0.0.1:8000/api/customers/?in_use=No -- This works http://127.0.0.1:8000/api/customers/?in_use=no -- Does not work because database stored this value as No

I think we have to use iexact option instead of exact, but not getting where to over ride this without impacting the original library.

Your assistance is helpful here

Thanks

miki725 commented 6 years ago

http://127.0.0.1:8000/api/customers/?in_use__iexact=no will work

rajesh-h commented 6 years ago

@miki725 Thank you I did see it works.

But is there a way to override in_use=no should be considered as iexact instead of exact.

I saw this method here for exact, I was thinking if I can override this on my side to iexact should work, but I was not successful here. :(

miki725 commented 6 years ago

you can do:

class CustomerFilterSet(FilterSet):
    in_use = Filter(form_field=forms.CharField(), default_lookup='iexact')
miki725 commented 6 years ago

relevant docs https://django-url-filter.readthedocs.io/en/latest/api/url_filter.filters.html#url_filter.filters.Filter

rajesh-h commented 6 years ago

@miki725 Thank you for the right direction. Is there a option to set default_lookup to iexact on api level or on model level?

Setting this on a field level is not a ideal situation in my case.

Once again thanks a lot for your help.

miki725 commented 6 years ago

what do you mean api level?

rajesh-h commented 6 years ago

@miki725 apologies i missed this comment.

When i say api level, i mean option to set all fields to case insensitive rather than setting per field basis like the example which is provided.
the example provided deals with fields level not on entire model level, so I was thinking an option to set on entire api level what should be the default filter exact or iexact or contains or icontains eg: default_filter = 'iexact' #this will be global setting

Thanks

miki725 commented 6 years ago

that could be possible although its not that simple. iexact only makes sense for string fields. so the default will only have to apply where iexact is possible. but yes that could definitely be added as an improvement