Fixes #7205: Handle null_option when getting selected form values in applied_filters template tag

This commit is contained in:
thatmattlove 2021-09-07 18:30:45 -07:00
parent 49617a595d
commit 752de0d9c0
2 changed files with 12 additions and 2 deletions

View File

@ -10,6 +10,7 @@
* [#7176](https://github.com/netbox-community/netbox/issues/7176) - Fix issue where query parameters were duplicated across different forms of the same type
* [#7188](https://github.com/netbox-community/netbox/issues/7188) - Fix issue where select fields with `null_option` did not render or send the null option
* [#7193](https://github.com/netbox-community/netbox/issues/7193) - Fix prefix (flat) template issue when viewing child prefixes with prefixes available
* [#7205](https://github.com/netbox-community/netbox/issues/7205) - Fix issue where selected fields with `null_option` set were not added to applied filters
---

View File

@ -120,11 +120,20 @@ def get_selected_values(form, field_name):
if not hasattr(form, 'cleaned_data'):
form.is_valid()
filter_data = form.cleaned_data.get(field_name)
field = form.fields[field_name]
# Selection field
if hasattr(form.fields[field_name], 'choices'):
if hasattr(field, 'choices'):
try:
choices = dict(unpack_grouped_choices(form.fields[field_name].choices))
grouped_choices = [(k, v) for k, v in field.choices]
if hasattr(field, 'null_option'):
# If the field has a `null_option` attribute set and it is selected,
# add it to the field's grouped choices.
if field.null_option is not None and field.null_option in filter_data:
grouped_choices.append((field.null_option, field.null_option))
choices = dict(unpack_grouped_choices(grouped_choices))
return [
label for value, label in choices.items() if str(value) in filter_data
]