Skip to content

Commit

Permalink
Fixes #1513: Correct filtering of custom field choices
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremystretch committed Oct 20, 2017
1 parent 85347d9 commit a0b93bb
Showing 1 changed file with 19 additions and 8 deletions.
27 changes: 19 additions & 8 deletions netbox/extras/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,28 @@ def __init__(self, cf_type, *args, **kwargs):
super(CustomFieldFilter, self).__init__(*args, **kwargs)

def filter(self, queryset, value):

# Skip filter on empty value
if not value.strip():
return queryset
# Treat 0 as None for Select fields
try:
if self.cf_type == CF_TYPE_SELECT and int(value) == 0:
return queryset.exclude(
custom_field_values__field__name=self.name,
)
except ValueError:
pass

# Selection fields get special treatment (values must be integers)
if self.cf_type == CF_TYPE_SELECT:
try:
# Treat 0 as None
if int(value) == 0:
return queryset.exclude(
custom_field_values__field__name=self.name,
)
# Match on exact CustomFieldChoice PK
else:
return queryset.filter(
custom_field_values__field__name=self.name,
custom_field_values__serialized_value=value,
)
except ValueError:
return queryset.none()

return queryset.filter(
custom_field_values__field__name=self.name,
custom_field_values__serialized_value__icontains=value,
Expand Down

0 comments on commit a0b93bb

Please sign in to comment.