fix: Listify initial field value when SearchableField.max_entries == 1 (#4951)

* fix: Listify initial field value when SearchableField.max_entries == 1

* fix: Don't convert initial=None to initial=[None] in has_changed()
This commit is contained in:
Jennifer Richards 2023-01-06 18:06:00 -04:00 committed by GitHub
parent de62fd72d9
commit 2993322958
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -320,6 +320,13 @@ class SearchableField(forms.MultipleChoiceField):
return objs.first() if self.max_entries == 1 else objs
def has_changed(self, initial, data):
# When max_entries == 1, we behave like a ChoiceField so initial will likely be a single
# value. Make it a list so MultipleChoiceField's has_changed() can work with it.
if initial is not None and self.max_entries == 1 and not isinstance(initial, (list, tuple)):
initial = [initial]
return super().has_changed(initial, data)
class IETFJSONField(jsonfield.fields.forms.JSONField):
def __init__(self, *args, empty_values=jsonfield.fields.forms.JSONField.empty_values,