django有多个选择,没有选项

时间:2014-10-14 15:05:45

标签: python django django-templates

将此视为模型

test = Industry.objects.all()

,结果是

[<Industry: test 1>, <Industry: test 2>, <Industry: test 3>]

我的模型字段是

options = models.ManyToManyField(Industry, default='')

在我的forms.py

options = forms.ModelMultipleChoiceField(widget=forms.CheckboxSelectMultiple(attrs={'class':'contract-text-fld'}), queryset=Industry.objects.all(), required=False)

众所周知,outpu表格看起来像是

checkbox1
checkbox1
checkbox1

但我需要这样的东西

label1      Yes(radio)     No(radio)
label2      Yes(radio)     No(radio)
label3      Yes(radio)     No(radio)

是否可以,如果我可以处理这个?

1 个答案:

答案 0 :(得分:1)

没有标准小工具以您希望的方式显示ManyToManyField。您需要编写一个自定义小部件来执行此操作。

基本小部件文档将非常有用: https://docs.djangoproject.com/en/dev/ref/forms/widgets/

有关如何编写自定义窗口小部件的教程:http://tothinkornottothink.com/post/10815277049/django-forms-i-custom-fields-and-widgets-in-detail

相关问题