Skip to content

Instantly share code, notes, and snippets.

@IlianIliev
Last active September 10, 2015 11:05
Show Gist options
  • Save IlianIliev/3941c1b031406bdf6657 to your computer and use it in GitHub Desktop.
Save IlianIliev/3941c1b031406bdf6657 to your computer and use it in GitHub Desktop.
DynamicChoiceField for Django REST Framework 3.x
class DynamicChoiceField(ChoiceField):
""" Privides set_choices method used to update all choice related attributes of the field """
def set_choices(self, choices):
""" Unfortunately just setting field.choices is not enough so we will sue this class to update evverything """
self.grouped_choices = to_choices_dict(choices)
self.choices = flatten_choices_dict(self.grouped_choices)
self.choice_strings_to_values = dict([
(six.text_type(key), key) for key in self.choices.keys()
])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment