Skip to content

Allow ChoiceField.choices to be set dynamically #5426

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 20, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 17 additions & 9 deletions rest_framework/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -1337,18 +1337,10 @@ class ChoiceField(Field):
html_cutoff_text = _('More than {count} items...')

def __init__(self, choices, **kwargs):
self.grouped_choices = to_choices_dict(choices)
self.choices = flatten_choices_dict(self.grouped_choices)
self.choices = choices
self.html_cutoff = kwargs.pop('html_cutoff', self.html_cutoff)
self.html_cutoff_text = kwargs.pop('html_cutoff_text', self.html_cutoff_text)

# Map the string representation of choices to the underlying value.
# Allows us to deal with eg. integer choices while supporting either
# integer or string input, but still get the correct datatype out.
self.choice_strings_to_values = {
six.text_type(key): key for key in self.choices.keys()
}

self.allow_blank = kwargs.pop('allow_blank', False)

super(ChoiceField, self).__init__(**kwargs)
Expand Down Expand Up @@ -1377,6 +1369,22 @@ def iter_options(self):
cutoff_text=self.html_cutoff_text
)

def _get_choices(self):
return self._choices

def _set_choices(self, choices):
self.grouped_choices = to_choices_dict(choices)
self._choices = flatten_choices_dict(self.grouped_choices)

# Map the string representation of choices to the underlying value.
# Allows us to deal with eg. integer choices while supporting either
# integer or string input, but still get the correct datatype out.
self.choice_strings_to_values = {
six.text_type(key): key for key in self.choices.keys()
}

choices = property(_get_choices, _set_choices)


class MultipleChoiceField(ChoiceField):
default_error_messages = {
Expand Down
13 changes: 13 additions & 0 deletions tests/test_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -1425,6 +1425,19 @@ def test_iter_options(self):

assert items[9].value == 'boolean'

def test_edit_choices(self):
field = serializers.ChoiceField(
allow_null=True,
choices=[
1, 2,
]
)
field.choices = [1]
assert field.run_validation(1) is 1
with pytest.raises(serializers.ValidationError) as exc_info:
field.run_validation(2)
assert exc_info.value.detail == ['"2" is not a valid choice.']


class TestChoiceFieldWithType(FieldValues):
"""
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy