django表单 - 呈现M2M的垂直复选框

时间:2013-01-16 14:02:23

标签: django django-models django-forms django-admin

我想使用M2M的垂直复选框。这是一个代码示例:

class Contact(models.Model):
    ...
    groups = models.ManyToManyField (
        Group,
        related_name = "contacts",
        verbose_name = _("Groups"),
    )
    ...

class Group (models.Model):
    name = models.CharField (
        unique = True,
        max_length = 100,
        db_index = True,
    )

class ContactAdminForm(forms.ModelForm):
    class Meta:
        model = Contact

    radio_fields = {"groups": admin.VERTICAL} # only for foreignkey or for choices

对它有任何帮助吗?

感谢, 诉

1 个答案:

答案 0 :(得分:0)

当您在表单中指定字段时,您可以更改窗口小部件,如下所示:

groups = forms.ModelMultipleChoiceField(queryset=Group.objects.all(), widget=forms.CheckboxSelectMultiple)