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

ModelChoiceFilter isnull not working #1309

Open
skikd636 opened this issue Dec 27, 2020 · 2 comments
Open

ModelChoiceFilter isnull not working #1309

skikd636 opened this issue Dec 27, 2020 · 2 comments

Comments

@skikd636
Copy link

When using the ModelChoiceFilter with an lookup_expr='isnull' django throws a value error exception
Cannot use None as a query value

This is caused by the ChoiceFilter.filter function that uses "None" instead of "True" as the value.

Line 169 in the filters.py module:
qs = self.get_method(qs)(**{'%s__%s' % (self.field_name, self.lookup_expr): None})

The ModelChoiceFilter should have it's own filter function defined as follows:

def filter(self, qs, value):
   if value != self.null_value:
      return super().filter(qs, value)
    
   qs = self.get_method(qs)(**{'%s__%s' % (self.field_name, self.lookup_expr): True})
   return qs.distinct() if self.distinct else qs
@AgentSingle

This comment has been minimized.

@skikd636
Copy link
Author

Looking at this more. The error does get thrown when lookup_expr='isnull' but I think I was missing the point. I think this is more of a documentation issue. I assumed when reading the documentation that I needed to set lookup_expr='isnull' when I wanted to add a Null lookup to a ModelChoiceField. I finally understand that is not the case. I just need to add a null_label='NULL' which add the null value to the ModelChoiceField.

That is not clear in the documentation. The filter works as expected (able to find empty fields) if I just set that value.

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

No branches or pull requests

3 participants