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

Pass a dict as data to django-filter #2529

Closed
wants to merge 2 commits into from

Conversation

buzzeante
Copy link

In order to filter with multivalue params such as 'in' lookups there are two alternatives:

  • ?multiparam=1,2,3
  • ?multiparam=1&multiparam=2

The second option is a better approach when facing strings. However, by definition QueryDict does only take into account the last param included.

Django-filters expect a dict as data. To be congruent with what is passed in the request we need to transform query_params to a dict.

As can be seen in the test written, adhoc programming must be done also in django-filter to allow 'in' lookups but that's another issue.

Part of the solution was taken from @leo-the-manic in http://stackoverflow.com/questions/13349573/how-to-change-a-django-querydict-to-python-dict#answer-22100334

In order to filter with multivalue params such as 'in' lookups there are two alternatives:

* ?multiparam=1,2,3
* ?multiparam=1&multiparam=2

The second option is a better approach when facing strings. However, by definition QueryDict does only take into account the last param included.

Django-filters expect a dict as data. To be congruent with what is passed in the request we need to transform `query_params` to a `dict`.

As can be seen in the test written, adhoc programming must be done also in django-filter to allow 'in' lookups but that's another issue.

Part of the solution was taken from @leo-the-manic in http://stackoverflow.com/questions/13349573/how-to-change-a-django-querydict-to-python-dict#answer-22100334
Change on transformation to make it compatible with python 2.6
@carltongibson
Copy link
Collaborator

by definition QueryDict does only take into account the last param included.

This isn't quite right. QueryDict already supports multiple params — you need to use getlist to access more than the last one.

Django Filter should do the right thing here. See carltongibson/django-filter#137. There's two things going on there:

  1. Support filtering on multiple values for the same param.
  2. Support the compact 1,2,3 syntax.

No 1. should already work. No 2 will likely appear at some point (but there's a solution already available if you want it yourself.)

If you can provide a failing test case on Django Filter I'll happily look at this. A fix is even better :-)

Either way, I don't think this is DRF's problem: I'll close this — shout if I've missed the point.

@buzzeante
Copy link
Author

I believe it's me who missed the point :$

You are completely right. I had a missleading interpretation of django-filters library. My apologies.

Awesome library btw. It's making my life easier :)

Cheers,
Javi

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

Successfully merging this pull request may close these issues.

2 participants