-
Notifications
You must be signed in to change notification settings - Fork 176
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
fixed isnull, istrue, isfalse lookups/operators #156
base: develop
Are you sure you want to change the base?
Changes from 2 commits
a0dc9d6
4f425e3
ab4f9df
7f74f64
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -96,12 +96,10 @@ def _build_query_dict(self, formdata=None): | |
if self.is_valid() and formdata is None: | ||
formdata = self.cleaned_data | ||
key = "{field}__{operator}".format(**formdata) | ||
if formdata['operator'] == "isnull": | ||
return {key: None} | ||
elif formdata['operator'] == "istrue": | ||
return {formdata['field']: True} | ||
elif formdata['operator'] == "isfalse": | ||
return {formdata['field']: False} | ||
|
||
if formdata['operator'] in ["isnull", "istrue", "isfalse"]: | ||
return {key: True if str(formdata['value']).lower() in ['1', 'true'] else False} | ||
|
||
return {key: formdata['value']} | ||
|
||
@staticmethod | ||
|
@@ -115,6 +113,11 @@ def _parse_query_dict(query_data, model): | |
return query_data | ||
|
||
parts = query_data['field'].split('__') | ||
boolean_lookups = ['isnull', 'istrue', 'isfalse'] | ||
for boolean_lookup in boolean_lookups: | ||
if query_data['field'].endswith(f'__{boolean_lookup}'): | ||
query_data['operator'] = boolean_lookup | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This seems identical to looking up the operator in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Correct, should be the same. I don't remember the purpose of these lines of mine but I tested it again and it works exactly as your code so we can delete this part. Thanks for double check. |
||
|
||
if len(parts) < 2: | ||
field = parts[0] | ||
else: | ||
|
@@ -132,18 +135,11 @@ def _parse_query_dict(query_data, model): | |
else: | ||
mfield = mfield[-1] # get the field object | ||
|
||
if query_data['value'] is None: | ||
query_data['operator'] = "isnull" | ||
elif query_data['value'] is True: | ||
query_data['operator'] = "istrue" | ||
elif query_data['value'] is False: | ||
query_data['operator'] = "isfalse" | ||
if isinstance(mfield, DateField): | ||
# this is a date/datetime field | ||
query_data['operator'] = "range" # default | ||
else: | ||
if isinstance(mfield, DateField): | ||
# this is a date/datetime field | ||
query_data['operator'] = "range" # default | ||
else: | ||
query_data['operator'] = operator # default | ||
query_data['operator'] = operator # default | ||
|
||
if isinstance(query_data.get('value'), | ||
list) and query_data['operator'] == 'range': | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this functionally identical to before, or did something change here? Or is the change that
isnull
operator should return{use key: True}
?From a readability perspective, I think I prefer the previous code
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, this is the most important part. Otherwise it returns
'True'
value (string) instead ofTrue
(boolean), which is incorrect and produces false SQL statement. That's the reason why I iterate only boolean operators ("isnull", "istrue", "isfalse") and not all of theAdvancedFilterQueryForm.OPERATORS
.Except of that. It allows to combine values like following:
which can be useful as well.