[英]Select a valid choice. ["objects"] is not one of the available choices. in Django
[英]Select a valid choice.[<selection>]is not one of the available choices. Error
我想要的是选择保存并提交给数据库的多选答案
但是我在每个问题上都会遇到此错误。
我的models.py
class preferences(models.Model):
cuisine = (
(1,u'Italian'),
(2,u'American'),
(3,u'French'),
(4,u'Japanese'),
(5,u'Russian'),
(6,u'Chinese'),
(7,u'Mexican'),
(8,u'Indian'),
(9,u'Middle Eastern'),
(10,u'Thai'),
(11,u'Spanish')
)
vegeterian = (
(1,u'Yes'),
(2,u'No'),
(3,u'I appreciate both')
)
lunch = (
(1,u'Cafe'),
(2,u'Restaurant'),
(3,u'Fast Food'),
(4,u'Takeaway'),
(5,u'Grocery/Lunch Box')
)
dinner = (
(1,u'Inexpensive Restaurant'),
(2,u'Fine Dining'),
(3,u'Takeaway'),
(4,u'Fast Food'),
(5,u'Delivery'),
(6,u'Cooking at home'),
(7,u'Cheeky Bar') #Make it with a hint that having a dinner w/ a cheeky pint
)
Friday = (
(1,u'Bar'),
(2,u'Night club'),
(3,u'Karaoke'),
(4,u'Netflix & chill'),
(5,u'Video games'),
(6,u'Cinema'),
(7,u'Theater'),
(8,u'Restaurant'),
)
weekend = (
(1,u'Hiking'),
(2,u'Sport activities'),
(3,u'Attending sport events'),
(4,u'Music events'),
(5,u'Art/Science exhibitions'),
(6,u'Chilling at the park'),
(7,u'Video games'),
(8,u'Cinema'),
(9,u'Theater'),
(10,u'Chilling at home')
)
UserID = models.ForeignKey(User,related_name='User', null=True,default='')
cuisine = models.ManyToManyField('self',choices=cuisine, max_length=20,blank=False,default='')
cuisine_extra = models.CharField(max_length=25)
vegeterian = models.CharField(max_length=15,choices=vegeterian)
lunch = models.ManyToManyField('self',choices=lunch,max_length=20,blank=False,default='')
dinner = models.ManyToManyField('self',choices=dinner,max_length=20,blank=False,default='')
Friday = models.ManyToManyField('self',choices=Friday,max_length=20,blank=False,default='')
weekend = models.ManyToManyField('self',choices=weekend,max_length=40,blank=False,default='')
在这里,我们使用M2Mfield
因为我们要存储多个项目,并在同一model('self')
上使用它。
modelform.py
class preferencesForm(ModelForm):
"""def __init__(self, *args, **kwargs):
super(preferencesForm, self).__init__(*args, **kwargs)"""
class Meta:
model = preferences
fields = ['cuisine', 'cuisine_extra', 'vegeterian', 'lunch', 'dinner', 'Friday', 'weekend']
widgets = {
'cuisine':forms.widgets.CheckboxSelectMultiple,
'lunch':forms.widgets.CheckboxSelectMultiple,
'dinner':forms.widgets.CheckboxSelectMultiple,
'Friday':forms.widgets.CheckboxSelectMultiple,
'weekend':forms.widgets.CheckboxSelectMultiple,
}
form = preferencesForm()
我尝试使用__init__
这样做,但这也没有帮助。 在这一点上我迷路了。
views.py
def display_form(request):
if not request.user.is_authenticated:
return redirect(settings.LOGIN_URL)
if request.method == 'POST':
form = preferencesForm(request.POST)
if form.is_valid():
form.UserID = request.user
form.save()
return HttpResponseRedirect('/')
else:
form = preferencesForm()
return render(request,'display.html',{'form':form})
views.py应该很好,但以防万一。
错误看起来像这样
选择一个有效的选择。 [u'3',u'9']不是可用的选择之一。
(选择取决于选择的选项的数量,但是想法是相同的)我也尝试将整数更改为字符串,但都没有帮助。 还尝试了[u'1']
等,也没有起作用。
我知道我可以使用multiselectfield库,但这对我的数据库不利,因为存储多个实例不是一个好主意。 我已经看到有很多类似的错误,但是没有一个对我有帮助:/非常感谢您的帮助! 谢谢!
您的字段没有意义。 多对多字段是模型之间的关系,其中每一边可以有多个项目。 它不是选项列表中的一组项目。
您应该将CommaSeparatedIntegerField与validate_comma_separated_integer_list
一起使用,以验证项目是否为整数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.