Skip to content

Commit

Permalink
Guard against superfluous "choices" param in filter class instantiation
Browse files Browse the repository at this point in the history
  • Loading branch information
bctiemann committed Sep 20, 2024
1 parent f239b23 commit e462e29
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions netbox/netbox/filtersets.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ def _get_filter_lookup_dict(existing_filter):
django_filters.ModelChoiceFilter,
django_filters.ModelMultipleChoiceFilter,
TagFilter
)) or existing_filter.extra.get('choices'):
)):
# These filter types support only negation
return FILTER_NEGATION_LOOKUP_MAP

Expand Down Expand Up @@ -172,21 +172,26 @@ def get_additional_lookups(cls, existing_filter_name, existing_filter):
# Create new filters for each lookup expression in the map
for lookup_name, lookup_expr in lookup_map.items():
new_filter_name = f'{existing_filter_name}__{lookup_name}'
existing_filter_extra = deepcopy(existing_filter.extra)

try:
if existing_filter_name in cls.declared_filters:
# The filter field has been explicitly defined on the filterset class so we must manually
# 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
resolve_field(field, lookup_expr) # Will raise FieldLookupError if the lookup is invalid
filter_cls = django_filters.BooleanFilter if lookup_expr == 'empty' else type(existing_filter)
if lookup_expr == 'empty':
existing_filter_extra.pop('choices', None)
filter_cls = django_filters.BooleanFilter
else:
filter_cls = type(existing_filter)
new_filter = filter_cls(
field_name=field_name,
lookup_expr=lookup_expr,
label=existing_filter.label,
exclude=existing_filter.exclude,
distinct=existing_filter.distinct,
**existing_filter.extra
**existing_filter_extra
)
elif hasattr(existing_filter, 'custom_field'):
# Filter is for a custom field
Expand Down

0 comments on commit e462e29

Please sign in to comment.