在Django上提交表单时出现CheckboxSelectMultiple验证错误 [英] CheckboxSelectMultiple validation error when submitting the form on Django

查看:68
本文介绍了在Django上提交表单时出现CheckboxSelectMultiple验证错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

models.py

models.py

class MyModel(models.Model):
      OPTION_CHOICES = (('a','a'),('b','b'))
      option = models.CharField(max_length=1, choices=OPTION_CHOICES)

forms.py

class MyForm(ModelForm):
      class Meta:
            model=MyModel
            fields=['option']
            widgets = {'option':CheckboxSelectMultiple(),}

当我尝试提交表单时,出现验证错误,无法提交。当我有机会 CheckboxSelectMultiple RadioSelect 时,它就可以正常工作。因此,如何使用 checkboxSelectMultiple

When I try to submit the form, I have validation error and can't submit it. When I chance CheckboxSelectMultiple to RadioSelect it works just fine. So how can I fix this using checkboxSelectMultiple

推荐答案

option = models.CharField(max_length=1, choices=OPTION_CHOICES)

仅接受一个字符。通过提交带有多选小部件的表单,您尝试存储列表:

Only accepts a single char. By submitting the form with multiple choice widget you try to store a list:

[u'a']

这当然会导致错误:

Select a valid choice. [u'a'] is not one of the available choices.

再次:'a''b'是字符串和有效的选择, [u'a'] 是一个列表,无效。

Again: 'a' or 'b' are strings and valid choices, [u'a'] is a list an is not valid.

要存储列表(或多个关系),您应该选择其他字段类型。哪种字段类型完全取决于您的项目要求。没有足够的信息来建议您使用什么。

To store lists (or multiple relations) you should pick some other field type. What field type exactly depends on your project requirements. There is not enough info to give you advice what to use.

这篇关于在Django上提交表单时出现CheckboxSelectMultiple验证错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆