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

Search NULL values on all field types #639

Closed
wants to merge 7 commits into from

Conversation

michael-mri
Copy link

No description provided.

@michael-mri
Copy link
Author

Few things to note:

  • new setting NULL_VALUE which is the default null value. NULL_CHOICE_VALUE defaults to it.
  • haven't yet implemented tests for each type of field
  • haven't yet updated the documentation
  • we may want to add null value in forms some way (especially for ModelChoiceFilter), although I am not sure how....

I struggled a bit on making form validation work: see filterset.py, lines 222-227.

Comments and suggestions are welcome :)

@rpkilby
Copy link
Collaborator

rpkilby commented Feb 22, 2017

I can provide more details, but I'm kind of -1 on these changes since it will play poorly with non-text inputs. My recommendation is to use a separate filter to handle null values explicitly, as demonstrated in the docs.

An example of poor form usage would be number inputs. The html number input does not allow text entry, so it's not possible to submit the 'null' string. If you manually coerce the querystring to filter by the null string, then the form field will have to be altered to a text input. When re-rendering the form, the number controls will not be present. This also applies to date, datetime, and custom fields (such as any MultiValueFields).

@michael-mri
Copy link
Author

michael-mri commented Feb 23, 2017

Indeed I agree they play poorly when it comes to use them in inputs, especially those generated automatically on DRF web interface for example.
However this make REST interface more friendly, being able to filter for example author=1, author=2 or author=null the same way.

I think this also makes sense for choices filters (ChoiceFilter, ModelChoiceFilter, MultipleChoiceFilter and ModelMultipleChoiceFilter) because these are rendered using input like selectboxes where we can define what null value is.

Maybe null_value should be restricted to those filters ?

@rpkilby
Copy link
Collaborator

rpkilby commented Feb 23, 2017

I think this also makes sense for choices filters (ChoiceFilter, ModelChoiceFilter, MultipleChoiceFilter and ModelMultipleChoiceFilter) because these are rendered using input like selectboxes where we can define what null value is.

Setting NULL_CHOICE_LABEL to a non-None value should enable null value filtering for both ChoiceFilter and ModelChoiceFilter.

If I recall correctly, null-choice filtering was not added to MultipleChoiceFilter since it would be generally non-sensical. eg, I want to filter all blogs that have no entries and that have this specific entry. You would end up with a contradictory query.

@carltongibson
Copy link
Owner

all blogs that have no entries OR that have this specific entry would make sense though right?

@rpkilby
Copy link
Collaborator

rpkilby commented Feb 26, 2017

all blogs that have no entries OR that have this specific entry would make sense though right?

True, and it would not be much effort to extend the existing functionality it to MultipleChoiceFilter and ModelMultipleChoiceFilter.

@codecov-io
Copy link

codecov-io commented Mar 7, 2017

Codecov Report

❗ No coverage uploaded for pull request base (develop@38df0c3). Click here to learn what that means.
The diff coverage is 100%.

@@            Coverage Diff             @@
##             develop     #639   +/-   ##
==========================================
  Coverage           ?   97.81%           
==========================================
  Files              ?       15           
  Lines              ?     1096           
  Branches           ?        0           
==========================================
  Hits               ?     1072           
  Misses             ?       24           
  Partials           ?        0
Impacted Files Coverage Δ
django_filters/conf.py 97.22% <ø> (ø)
django_filters/filterset.py 100% <ø> (ø)
django_filters/filters.py 98.56% <100%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 38df0c3...58e70ba. Read the comment docs.

Copy link
Owner

@carltongibson carltongibson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I need to make a little time to review fully but in general I quite like this. (I like the idea and the implementation looks pretty light at first glance.)

I think we probably need to review the usage docs here. (There's already a good section on filtering for null values, it probably just needs expanding.)

Also, if you rebase this is Codecov happier? (There should be a base coverage report on develop now.)

@@ -222,7 +226,7 @@ def form(self):
fields = OrderedDict([
(name, filter_.field)
for name, filter_ in six.iteritems(self.filters)])

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you set your editor to trim trailing whitespace?

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.

4 participants