Added filters that filter multiple values at once using multiwidgets and multivaluefields #1164
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This is a pretty basic implementation, but it can do a few fancy things like efficiently filtering to customers with at least one completed, unreturned purchase:
Currently django-filter with two separate separate filters would return all customers with a completed purchase and an unreturned purchase (with no guarantee that they are the same purchase). See #745 and #1077 :
This is similar to
RelatedFilter
in philipn/django-rest-framework-filters/pull/199, but without the use of subqueries which can be extremely slow for this sort of lookup.Having done all this, I'm not sure a
MultiWidget
is a good way to group filters. The main issue is that validation errors are always described at theMultiWidget
level instead of the subwidget level (see https://stackoverflow.com/questions/12245887/how-to-render-validation-errors-in-a-multivaluefield-multiwidget-per-field). This makes validation error messages largely useless. Unfortunately this is a fairly core issue in django and not easily fixable.The django-rest-framework-filters approach of having a separate form for each
RelatedFilter
might be better.If people really like this
MultiWidget
approach, the remaining work is:ModelChoiceField
(currently errors due to no queryset)SuffixedMultiWidget
template to show suffixes to the userMultiValueField.clean
to raise more useful validation errors. The best approach is probably to to make errors a dictionary, but I'm not sure how to set the keys or howForm
will interpret a dictionary.I use django-rest-framework, so I don't have much experience with widgets, fields, and forms.