我正在尝试在表单中使用 CheckboxSelectMultiple,而不是 SelectMultiple 默认值。我尝试了两种不同的方法让它显示为复选框,但它们都在我的本地网页上显示默认的 SelectMultiple。其他一切都正确呈现( {{ form.as_table }} )。有人会指出我正确的方向吗?谢谢,
Django 版本 3.0.2
蟒蛇 3.6.9
模型.py
class MyModel(models.Model):
m2m = models.ManyToManyField('OtherModel', blank=True)
[...]
表格.py
from django.forms import ModelForm
from myapp.models import MyModel
class MyModelModelForm(ModelForm):
[...]
# I tried this...
m2m = forms.MultipleChoiceField(widget=forms.CheckboxSelectMultiple)
class Meta:
model = MyModel
fields = '__all__'
# or this...
widgets = {
'm2m': forms.CheckboxSelectMultiple,
}
视图.py
from django.views.generic.edit import CreateView
from myapp.models import MyModel
class MyModelCreate(CreateView):
model = MyModel
fields = '__all__'
template_name = 'MyApp/mymodel_form.html'