我正在尝试建模调查。
models.py
class Survey(models.Model):
Student = models.ForeignKey(Student)
OPTIONS = (
('0','Behavior 1'),
('1','Behavior 2'),
('2','Behavior 3'),
)
behaviors = models.SmallIntegerField(choices=OPTIONS)
how_cool = models.SmallIntegerField() # rating from 1 to 5
我想将行为显示为一组复选框,而不是默认下拉列表。我需要编写自定义模型字段吗?
创建一个ModelForm
,将行为指定为复选框:
from django.forms import ModelForm, CheckboxInput
class SurveyForm(ModelForm):
behaviors = forms.ChoiceField(choices=OPTIONS, widget=CheckboxInput)
现在您可以在管理员或CBV中使用此表格将行为作为复选框渲染。
也在这里查看:https://docs.djangoproject.com/en/dev/ref/forms/firps/fields/#widget