Last active
September 10, 2015 11:05
-
-
Save IlianIliev/3941c1b031406bdf6657 to your computer and use it in GitHub Desktop.
DynamicChoiceField for Django REST Framework 3.x
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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