我跟着this,但我仍然无法显示CHOICES作为我的窗体上的复选框。Django - 如何显示模型的选择作为复选框
models.py
class Car(models.Model):
TYPE_CHOICES = (
('s', 'small'),
('m', 'medium'),
('b', 'big'),
)
type = models.CharField(max_length=1, choices=TYPE_CHOICES)
forms.py
from django import forms
from django.forms.widgets import CheckboxSelectMultiple
from cars.models import Car
class AddCar(forms.ModelForm):
class Meta:
model = Car
type = forms.MultipleChoiceField(choices=Car.TYPE_CHOICES, widget=forms.CheckboxSelectMultiple())
他可以使用复选框输入。应该使用[this](https://docs.djangoproject.com/en/dev/ref/forms/widgets/#django.forms.CheckboxInput)。所以在你的例子中可以使用:'widgets = {'type':forms.CheckboxInput}' – marianobianchi
@marianobianchi恐怕他不能,'CheckboxInput'小部件用于布尔值。 – okm