ManyToMany Django

时间:2013-10-28 21:37:26

标签: python django

我正在尝试在表单上选中多个复选框。我正在使用以下方法,使用ManytoManyFields使用一系列选项填充表。使用脚本将选择复制到表中。但是在选中复选框后,我收到以下错误:

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 %}

0 个答案:

没有答案