我正在尝试在表单上选择多个复选框。我正在使用以下方法来使用 ManytoManyFields 填充具有一系列选择的表。CHOICES 使用脚本复制到表中。但是,在选择复选框后,我收到以下错误:
Select a valid choice. [u'_CU', u'_EN'] is not one of the available choices.
在模型中:
class A(models.Model):
MyPersonalityType = models.ManyToManyField(P)
CHOICES = (
('_NN', 'Choice 1...'),
('_GK', ''),
('_SA', ''),
('_ES', ''),
('_SH', ''),
('_CU', ''),
('_EN', ''),
)
class P(models.Model):
slug = models.CharField(primary_key=True, max_length=32)
title = models.CharField(unique=True, max_length=124)
在表格中:
MyPersonalityType = forms.ChoiceField(choices=CHOICES, widget=forms.CheckboxSelectMultiple())
我添加代码:
for tag,des in CHOICES:
new = P(slug=tag, title=des)
new.save()
模板是:
{% for field in form %}
<div class="fieldWrapper">
{{ field.errors }}
{{ field.label_tag }}: {{ field }}
</div>
{% endfor %}