django-url-filter icon indicating copy to clipboard operation
django-url-filter copied to clipboard

Question - How to make case insensitive filter?

Open rajesh-h opened this issue 7 years ago • 8 comments

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

rajesh-h avatar Mar 14 '18 13:03 rajesh-h

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

miki725 avatar Mar 16 '18 03:03 miki725

@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. :(

rajesh-h avatar Mar 16 '18 04:03 rajesh-h

you can do:

class CustomerFilterSet(FilterSet):
    in_use = Filter(form_field=forms.CharField(), default_lookup='iexact')

miki725 avatar Mar 16 '18 14:03 miki725

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

miki725 avatar Mar 17 '18 02:03 miki725

@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.

rajesh-h avatar Mar 17 '18 18:03 rajesh-h

what do you mean api level?

miki725 avatar Mar 21 '18 23:03 miki725

@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

rajesh-h avatar Apr 19 '18 20:04 rajesh-h

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

miki725 avatar Apr 19 '18 21:04 miki725