mirror of
https://github.com/netbox-community/netbox.git
synced 2025-08-26 09:16:10 -06:00
Always use BooleanFilter for 'empty' lookups
This commit is contained in:
parent
93a80c6580
commit
f632a536d4
@ -6,16 +6,12 @@ class Empty(Lookup):
|
|||||||
Filter on whether a string is empty.
|
Filter on whether a string is empty.
|
||||||
"""
|
"""
|
||||||
lookup_name = 'empty'
|
lookup_name = 'empty'
|
||||||
prepare_rhs = False
|
|
||||||
|
|
||||||
def as_sql(self, compiler, connection):
|
def as_sql(self, qn, connection):
|
||||||
if not isinstance(self.rhs, bool):
|
lhs, lhs_params = self.process_lhs(qn, connection)
|
||||||
raise ValueError("The QuerySet value for an empty lookup must be True or False.")
|
rhs, rhs_params = self.process_rhs(qn, connection)
|
||||||
sql, params = compiler.compile(self.lhs)
|
params = lhs_params + rhs_params
|
||||||
if self.rhs:
|
return 'CAST(LENGTH(%s) AS BOOLEAN) != %s' % (lhs, rhs), params
|
||||||
return f"CAST(LENGTH({sql}) AS BOOLEAN) IS NOT TRUE", params
|
|
||||||
else:
|
|
||||||
return f"CAST(LENGTH({sql}) AS BOOLEAN) IS TRUE", params
|
|
||||||
|
|
||||||
|
|
||||||
CharField.register_lookup(Empty)
|
CharField.register_lookup(Empty)
|
||||||
|
@ -177,7 +177,8 @@ class BaseFilterSet(django_filters.FilterSet):
|
|||||||
# create the new filter with the same type because there is no guarantee the defined type
|
# create the new filter with the same type because there is no guarantee the defined type
|
||||||
# is the same as the default type for the field
|
# is the same as the default type for the field
|
||||||
resolve_field(field, lookup_expr) # Will raise FieldLookupError if the lookup is invalid
|
resolve_field(field, lookup_expr) # Will raise FieldLookupError if the lookup is invalid
|
||||||
new_filter = type(existing_filter)(
|
filter_cls = django_filters.BooleanFilter if lookup_expr == 'empty' else type(existing_filter)
|
||||||
|
new_filter = filter_cls(
|
||||||
field_name=field_name,
|
field_name=field_name,
|
||||||
lookup_expr=lookup_expr,
|
lookup_expr=lookup_expr,
|
||||||
label=existing_filter.label,
|
label=existing_filter.label,
|
||||||
|
Loading…
Reference in New Issue
Block a user