Fixes #13849: Fix label resolution during serialization for removed field choices

This commit is contained in:
Jeremy Stretch 2023-09-21 14:21:13 -04:00
parent 9b325f4b86
commit 2a9b58925c

View File

@ -46,12 +46,14 @@ class ChoiceField(serializers.Field):
return super().validate_empty_values(data) return super().validate_empty_values(data)
def to_representation(self, obj): def to_representation(self, obj):
if obj == '': if obj != '':
return None # Use an empty string in place of the choice label if it cannot be resolved (i.e. because a previously
return { # configured choice has been removed from FIELD_CHOICES).
'value': obj, label = self._choices.get(obj, '')
'label': self._choices[obj], return {
} 'value': obj,
'label': label,
}
def to_internal_value(self, data): def to_internal_value(self, data):
if data == '': if data == '':