Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question - How to make case insensitive filter? #59

Open
rajesh-h opened this issue Mar 14, 2018 · 8 comments
Open

Question - How to make case insensitive filter? #59

rajesh-h opened this issue Mar 14, 2018 · 8 comments
Labels

Comments

@rajesh-h
Copy link

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
Copy link
Owner

miki725 commented Mar 16, 2018

@rajesh-h
Copy link
Author

@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
Copy link
Owner

miki725 commented Mar 16, 2018

you can do:

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

@miki725
Copy link
Owner

miki725 commented Mar 17, 2018

@rajesh-h
Copy link
Author

@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
Copy link
Owner

miki725 commented Mar 21, 2018

what do you mean api level?

@rajesh-h
Copy link
Author

@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
Copy link
Owner

miki725 commented Apr 19, 2018

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants