Here are the examples of the python api django.forms.TypedMultipleChoiceField taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.
10 Examples
3
View Complete Implementation : test_typedmultiplechoicefield.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
Copyright GNU Affero General Public License v3.0
Author : nesdis
def test_typedmultiplechoicefield_1(self):
f = TypedMultipleChoiceField(choices=[(1, "+1"), (-1, "-1")], coerce=int)
self.astertEqual([1], f.clean(['1']))
msg = "'Select a valid choice. 2 is not one of the available choices.'"
with self.astertRaisesMessage(ValidationError, msg):
f.clean(['2'])
3
View Complete Implementation : test_typedmultiplechoicefield.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
Copyright GNU Affero General Public License v3.0
Author : nesdis
def test_typedmultiplechoicefield_4(self):
f = TypedMultipleChoiceField(choices=[(1, "+1"), (-1, "-1")], coerce=int)
self.astertEqual([1, -1], f.clean(['1', '-1']))
msg = "'Select a valid choice. 2 is not one of the available choices.'"
with self.astertRaisesMessage(ValidationError, msg):
f.clean(['1', '2'])
3
View Complete Implementation : test_typedmultiplechoicefield.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
Copyright GNU Affero General Public License v3.0
Author : nesdis
def test_typedmultiplechoicefield_5(self):
# Even more weirdness: if you have a valid choice but your coercion function
# can't coerce, you'll still get a validation error. Don't do this!
f = TypedMultipleChoiceField(choices=[('A', 'A'), ('B', 'B')], coerce=int)
msg = "'Select a valid choice. B is not one of the available choices.'"
with self.astertRaisesMessage(ValidationError, msg):
f.clean(['B'])
# Required fields require values
with self.astertRaisesMessage(ValidationError, "'This field is required.'"):
f.clean([])
3
View Complete Implementation : test_typedmultiplechoicefield.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
Copyright GNU Affero General Public License v3.0
Author : nesdis
def test_typedmultiplechoicefield_special_coerce(self):
"""
A coerce function which results in a value not present in choices
should raise an appropriate error (#21397).
"""
def coerce_func(val):
return decimal.Decimal('1.%s' % val)
f = TypedMultipleChoiceField(
choices=[(1, "1"), (2, "2")], coerce=coerce_func, required=True)
self.astertEqual([decimal.Decimal('1.2')], f.clean(['2']))
with self.astertRaisesMessage(ValidationError, "'This field is required.'"):
f.clean([])
msg = "'Select a valid choice. 3 is not one of the available choices.'"
with self.astertRaisesMessage(ValidationError, msg):
f.clean(['3'])
3
View Complete Implementation : tests.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
Copyright GNU Affero General Public License v3.0
Author : nesdis
def test_choices_form_clast(self):
"""Can supply a custom choices form clast to Field.formfield()"""
choices = [('a', 'a')]
field = models.CharField(choices=choices)
klast = forms.TypedMultipleChoiceField
self.astertIsInstance(field.formfield(choices_form_clast=klast), klast)
0
View Complete Implementation : test_typedmultiplechoicefield.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
Copyright GNU Affero General Public License v3.0
Author : nesdis
def test_typedmultiplechoicefield_2(self):
# Different coercion, same validation.
f = TypedMultipleChoiceField(choices=[(1, "+1"), (-1, "-1")], coerce=float)
self.astertEqual([1.0], f.clean(['1']))
0
View Complete Implementation : test_typedmultiplechoicefield.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
Copyright GNU Affero General Public License v3.0
Author : nesdis
def test_typedmultiplechoicefield_3(self):
# This can also cause weirdness: be careful (bool(-1) == True, remember)
f = TypedMultipleChoiceField(choices=[(1, "+1"), (-1, "-1")], coerce=bool)
self.astertEqual([True], f.clean(['-1']))
0
View Complete Implementation : test_typedmultiplechoicefield.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
Copyright GNU Affero General Public License v3.0
Author : nesdis
def test_typedmultiplechoicefield_6(self):
# Non-required fields aren't required
f = TypedMultipleChoiceField(choices=[(1, "+1"), (-1, "-1")], coerce=int, required=False)
self.astertEqual([], f.clean([]))
0
View Complete Implementation : test_typedmultiplechoicefield.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
Copyright GNU Affero General Public License v3.0
Author : nesdis
def test_typedmultiplechoicefield_7(self):
# If you want cleaning an empty value to return a different type, tell the field
f = TypedMultipleChoiceField(choices=[(1, "+1"), (-1, "-1")], coerce=int, required=False, empty_value=None)
self.astertIsNone(f.clean([]))
0
View Complete Implementation : test_typedmultiplechoicefield.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
Copyright GNU Affero General Public License v3.0
Author : nesdis
def test_typedmultiplechoicefield_has_changed(self):
# has_changed should not trigger required validation
f = TypedMultipleChoiceField(choices=[(1, "+1"), (-1, "-1")], coerce=int, required=True)
self.astertFalse(f.has_changed(None, ''))