mirror of
https://github.com/netbox-community/netbox.git
synced 2025-07-24 17:38:37 -06:00
* Use BooleanFilter for 'empty' lookups * Always use BooleanFilter for 'empty' lookups * Restore Empty lookup logic
This commit is contained in:
parent
bf1c191b2e
commit
24a51dd86e
@ -7,12 +7,14 @@ 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)
|
sql, params = compiler.compile(self.lhs)
|
||||||
rhs, rhs_params = self.process_rhs(qn, connection)
|
if self.rhs:
|
||||||
params = lhs_params + rhs_params
|
return f"CAST(LENGTH({sql}) AS BOOLEAN) IS NOT TRUE", params
|
||||||
return 'CAST(LENGTH(%s) AS BOOLEAN) != %s' % (lhs, rhs), params
|
else:
|
||||||
|
return f"CAST(LENGTH({sql}) AS BOOLEAN) IS TRUE", params
|
||||||
|
|
||||||
|
|
||||||
class NetContainsOrEquals(Lookup):
|
class NetContainsOrEquals(Lookup):
|
||||||
|
@ -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,
|
||||||
@ -224,6 +225,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