Skip to content

Conversation

@thatmattlove
Copy link
Contributor

@thatmattlove thatmattlove commented Sep 8, 2021

Fixes: #7205

  • Apply same logic from DynamicModelChoiceField.clean() to DynamicModelMultipleChoiceField field so that None is added to cleaned form data
  • More importantly, use a field's null_option for applied_filters template tag when selected. For example, if 'Global' is selected, the filter should show 'Global'. After my fix from 2a293d5 in APISelect, applied_filters is now broken when a field with a null_option is selected.

@thatmattlove thatmattlove marked this pull request as draft September 8, 2021 01:36
@thatmattlove thatmattlove marked this pull request as ready for review September 8, 2021 01:55
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]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line can be omitted. We should call unpack_grouped_choices() first to flatten any grouped choices, then append the null choice as needed.

# 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 None in filter_data:
grouped_choices.append((field.null_option, field.null_option))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even though it's not actually referenced, we should probably set the value for the null choice to settings.FILTERS_NULL_CHOICE_VALUE just for clarity.

@jeremystretch jeremystretch merged commit 26ceeb6 into develop Sep 8, 2021
@jeremystretch jeremystretch deleted the 7205-applied-filters branch September 8, 2021 15:25
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 22, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

applied_filters template tag does not handle null_option on form fields

3 participants