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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

'__in' lookups don't work with CharFilter #2

Closed
jameswyse opened this issue Jan 11, 2017 · 4 comments
Closed

'__in' lookups don't work with CharFilter #2

jameswyse opened this issue Jan 11, 2017 · 4 comments
Milestone

Comments

@jameswyse
Copy link

jameswyse commented Jan 11, 2017

CharFilter doesn't seem to work with __in lookups.. the array gets converted to a string when CharFilter calls clean, so field__in=['one', 'two'] becomes field__in="['one', 'two']" 馃槶

Is this a bug? Either way I switched to using Filter and it's working great :)

Here's an example:

Filters

filters = (
    CharFilter('foo__bar', lookups=['exact', 'in']),
    Filter('bar__baz', lookups=['exact', 'in']),
)

Using CharFilter:

filters.parse(MultiValueDict({
    'foo__bar__in': ['one', 'two', 'three']
}))

# Results:
filters.valid == True
filters.Q == <Q: (AND: ("foo__bar__in", "['one', 'two', 'three']"))>

Using Filter:

filters.parse(MultiValueDict({
    'bar__baz__in': ['nine', 'ten', 'eleven']
}))

# Results
filters.valid == True
filters.Q == <Q: (AND: ("bar__baz__in", ['nine', 'ten', 'eleven']))>
@bennullgraham
Copy link
Owner

Thanks for the detailed report! Yes, this is a bug. I came across this recently and it had been so long since writing the library that I got confused by it myself!

CharFilter currently uses Django's forms.CharField to convert the raw query input into a Python object before Filternaut works with it. With CharFilter this is basically a no-op since it runs str() in the input and the input is already a string, but if you use e.g. DateTimeFilter then raw input of '2017-01-01' is turned into a datetime instance, which is useful.

Anyway, this falls apart with multi-valued input as the conversion is applied to the raw input as-is; so CharFilter ends up running str() on ['one', 'two', 'three'] and you get the result described above.

If you're interested in making a patch, https://github.com/bennullgraham/filternaut/blob/master/filternaut/filters.py#L44 probably needs to special-case input which appears to be a list, tuple, etc. Otherwise, I'll fix when I get a chance!

@bennullgraham
Copy link
Owner

Turns out I got a chance fairly quickly :)

@jameswyse can you check that the fix/multi-clean branch (https://github.com/bennullgraham/filternaut/tree/fix/multi-clean) fixes or would appear to fix your case?

@bennullgraham bennullgraham added this to the 0.0.9 milestone Jan 13, 2017
@jameswyse
Copy link
Author

Thanks for the detailed response! (and then for fixing it!)

Your branch does appear to fix the problem I was seeing 馃榿

@bennullgraham
Copy link
Owner

Fixed in 0.0.9 which is now on PyPI. Thanks again for the ticket 馃憤

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

No branches or pull requests

2 participants