From f87b339245df2bd21e858ae439dfc3321eda0b2c Mon Sep 17 00:00:00 2001 From: Tanner Prestegard <tanner.prestegard@ligo.org> Date: Thu, 12 Jul 2018 10:54:25 -0500 Subject: [PATCH] New choice-type field that shows display value Created ChoiceDisplayField - same as ChoiceField, except its to_representation() used the display value instead of the key. --- gracedb/superevents/api/fields.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/gracedb/superevents/api/fields.py b/gracedb/superevents/api/fields.py index f2a4b7f1d..2cce706c6 100644 --- a/gracedb/superevents/api/fields.py +++ b/gracedb/superevents/api/fields.py @@ -64,3 +64,12 @@ class CommaSeparatedOrListField(fields.ListField): data = data[0].split(',') return super(CommaSeparatedOrListField, self).to_internal_value(data) + +class ChoiceDisplayField(fields.ChoiceField): + """ + Same as standard choice field, but return a choice's display_value + instead of the key when serializing the field. + """ + + def to_representation(self, value): + return self._choices[value] -- GitLab