Skip to content
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

copy SelectField.choices so each instance can be modified independently #286

Merged
merged 1 commit into from Jul 15, 2016
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 20 additions & 0 deletions tests/fields.py
Expand Up @@ -183,6 +183,26 @@ def test_render_kw(self):
u'<input foo="baz" id="a" name="a" other="hello" type="text" value="hello">'
)

def test_select_field_copies_choices(self):
class F(Form):
items = SelectField(choices=[])

def __init__(self, *args, **kwargs):
super(F, self).__init__(*args, **kwargs)

def add_choice(self, choice):
self.items.choices.append((choice, choice))

f1 = F()
f2 = F()

f1.add_choice('a')
f2.add_choice('b')

self.assertEqual(f1.items.choices, [('a', 'a')])
self.assertEqual(f2.items.choices, [('b', 'b')])
self.assertTrue(f1.items.choices is not f2.items.choices)


class PrePostTestField(TextField):
def pre_validate(self, form):
Expand Down
4 changes: 3 additions & 1 deletion wtforms/fields/core.py
Expand Up @@ -4,6 +4,8 @@
import decimal
import itertools

from copy import copy

from wtforms import widgets
from wtforms.compat import text_type, izip
from wtforms.i18n import DummyTranslations
Expand Down Expand Up @@ -445,7 +447,7 @@ class SelectField(SelectFieldBase):
def __init__(self, label=None, validators=None, coerce=text_type, choices=None, **kwargs):
super(SelectField, self).__init__(label, validators, **kwargs)
self.coerce = coerce
self.choices = choices
self.choices = copy(choices)

def iter_choices(self):
for value, label in self.choices:
Expand Down