10
什么时候应该使用具有强制函数的TypedChoiceField而不是使用ChoiceField上的字段形式的干净方法?Django中的TypedChoiceField或ChoiceField
换句话说,您为什么要使用MyForm而不是MyForm2,反之亦然。这只是一个偏好问题吗?
from django import forms
CHOICES = (('1', 'A'), ('2', 'B'), ('3', 'C'))
class MyForm(forms.Form):
my_field = ChoiceField(choices=CHOICES)
def clean_my_field(self):
value = self.cleaned_data['my_field']
return int(value)
class MyForm2(forms.Form):
my_field = TypedChoiceField(choices=CHOICES, coerce=int)