2010-09-09 12 views
10

필드의 양식에서 깨끗한 메서드를 사용하여 ChoiceField에서 강제 변환 함수로 TypedChoiceField를 사용해야하는 경우는 언제입니까?TypedChoiceField 또는 D 장고의 ChoiceField

다른 말로하면 MyForm2를 통해 MyForm을 사용하는 이유는 무엇입니까? 이것은 단순히 선호도의 문제입니까?

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) 

답변

11

나는 "무거워 짐"을하기 위해 clean_field 방법을 사용할 것입니다. 예를 들어 필드에 비정상적 인 커스텀 클리닝 및/또는 타입 변환 등이 필요한 경우. 반면에 요구 사항이 int으로 강제 변환되는 경우 똑같습니다. clean_field일 것입니다. 아마도입니다. TypedChoiceField은이 경우에 들어가는 길입니다.