mirror of
https://github.com/netbox-community/netbox.git
synced 2025-08-26 09:16:10 -06:00
Use BooleanFilter for 'empty' lookups
This commit is contained in:
parent
c36e7a1d0b
commit
93a80c6580
@ -6,12 +6,16 @@ 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, qn, connection):
|
def as_sql(self, compiler, connection):
|
||||||
lhs, lhs_params = self.process_lhs(qn, connection)
|
if not isinstance(self.rhs, bool):
|
||||||
rhs, rhs_params = self.process_rhs(qn, connection)
|
raise ValueError("The QuerySet value for an empty lookup must be True or False.")
|
||||||
params = lhs_params + rhs_params
|
sql, params = compiler.compile(self.lhs)
|
||||||
return 'CAST(LENGTH(%s) AS BOOLEAN) != %s' % (lhs, rhs), params
|
if self.rhs:
|
||||||
|
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)
|
||||||
|
@ -224,6 +224,14 @@ class BaseFilterSet(django_filters.FilterSet):
|
|||||||
|
|
||||||
return filters
|
return filters
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def filter_for_lookup(cls, field, lookup_type):
|
||||||
|
|
||||||
|
if lookup_type == 'empty':
|
||||||
|
return django_filters.BooleanFilter, {}
|
||||||
|
|
||||||
|
return super().filter_for_lookup(field, lookup_type)
|
||||||
|
|
||||||
|
|
||||||
class ChangeLoggedModelFilterSet(BaseFilterSet):
|
class ChangeLoggedModelFilterSet(BaseFilterSet):
|
||||||
"""
|
"""
|
||||||
|
Loading…
Reference in New Issue
Block a user