Django - 表单小部件 TypeChoiceField - 如何设置两个单选按钮的 id

发布于 2024-12-22 20:09:17 字数 999 浏览 0 评论 0原文

我正在使用带有 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,但我还没有找到正确的方法。有什么想法吗?

谢谢-卢克

i'm using a form with a TypeChoiceField, this is the form's code:

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)
    )

now i'm trying to set a custom id for the two radio buttons displayed but i havent found yet the right way. any idea?

thx - Luke

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北城半夏 2024-12-29 20:09:17

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

You need to customize the renderer HorizRadioRenderer's render method, as you need to customize it for each set of radio buttons. If it was just a single field, you could have used the attrs argument to customize the id.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文