Django - Forms Widget TypeChoiceField - 如何为两个单选按钮设置id

时间:2011-12-26 19:32:46

标签: django django-models django-forms django-templates django-views

我正在使用带有TypeChoiceField的表单,这是表单的代码:

class AnagraficaForm(forms.Form):
    usertype = ((1,'Privato'),(0,'Libero professionista/Azienda'))
    nome = forms.CharField(max_length=100)
    cognome = forms.CharField(max_length=100)
    telefono = forms.CharField(max_length=50,required=False)
    email= forms.EmailField(max_length=100,required=False)
    indirizzo = forms.CharField(max_length=100)
    cap = ITZipCodeField(required=False)
    citta = forms.CharField(max_length=100)
    codfisc = ITSocialSecurityNumberField(required=False)
    piva = ITVatNumberField(required=False)

    ragsociale = forms.CharField(max_length=100)
    is_privato = forms.TypedChoiceField(
        initial=1,
        coerce=lambda x: bool(int(x)),
        choices=usertype,
        #using custom renderer to display radio buttons on the same line
        widget=forms.RadioSelect(renderer=HorizRadioRenderer)
    )

现在我正在尝试为显示的两个单选按钮设置自定义ID,但我还没找到正确的方法。任何想法?

thx - Luke

1 个答案:

答案 0 :(得分:0)

您需要自定义渲染器HorizRadioRenderer的{​​{1}}方法,因为您需要为每组单选按钮自定义它。如果它只是一个字段,您可以使用attrs参数来自定义id。