Select 一个有效的选择。 ["objects"] 不是可用选项之一。在姜戈
Select a valid choice. ["objects"] is not one of the available choices. in Django
我将创建一个表单,用户可以在其中 select 来自选项的任意数量的对象。(MultipleChoiceField)
这是我的文件:
DAY_CHOICES = (
('saturday', 'شنبه'),
('sunday', 'یکشنبه'),
('monday', 'دوشنبه'),
('tuesday', 'سه شنبه'),
('wednesday', 'چهارشنبه'),
('thursday', 'پنج شنبه'),
('friday', 'جمعه'),)
# Models.py
class CorporationRequest(models.Model):
user = models.ForeignKey(User)
title = models.CharField(max_length=250, )
explain = models.CharField(max_length=500, )
assistance = models.CharField(max_length=250, choices=ASSISTANCE_CHOICES)
days = # MultipleChoiceField
created_date = models.DateTimeField( auto_now_add=True)
# Forms.py
class ObjectsForm(ModelForm):
objects = forms.MultipleChoiceField(widget=forms.CheckboxSelectMultiple,
choices=OBJECT_CHOICES)
class Meta:
model = ObjectModel
fields = '__all__'
# Views.py
def add_corporation(request)
if request.method == "POST":
form = ObjectsForm(request.POST)
if form.is_valid():
new_corporation = form.save(commit=False)
new_corporation.days = ','.join(form.cleaned_data['days'])
new_corporation.save()
return redirect('administrator:view_admin_user_corporation')
else:
form = ObjectsForm()
template = 'corporation.html'
context = {'form': form, }
return render(request, template, context)
当我点击提交按钮时(例如 select 对象 1 和 2),我得到这个错误:
Select a valid choice. ['1', '2'] is not one of the available choices.-
提示: 每个用户向我们发送请求,其中包含他可以做某事的解释和天数。因此,每个用户都可以 select 他想要的任何一天。
如果您必须将 CorporationRequest.days
保留为 CharField
,那么您可以将日期选择保存为字段中的逗号分隔字符串
class CorporationRequest(models.Model):
days = models.CharField(max_length=255)
您必须清理来自多选字段的数据,以便将其保存在该字段中
class CorporationRequestForm(ModelForm):
days = forms.MultipleChoiceField(widget=forms.CheckboxSelectMultiple,
choices=DAY_CHOICES)
class Meta:
model = CorporationRequest
fields = '__all__'
def clean_days(self):
return ','.join(self.cleaned_data['days'])
这意味着 CorporationRequest.days
现在是一个字符串而不是日期列表。您可以将 CorporationRequest
模型的方法添加到 return 天数列表
def get_days(self):
return self.days.split(',')
我将创建一个表单,用户可以在其中 select 来自选项的任意数量的对象。(MultipleChoiceField)
这是我的文件:
DAY_CHOICES = (
('saturday', 'شنبه'),
('sunday', 'یکشنبه'),
('monday', 'دوشنبه'),
('tuesday', 'سه شنبه'),
('wednesday', 'چهارشنبه'),
('thursday', 'پنج شنبه'),
('friday', 'جمعه'),)
# Models.py
class CorporationRequest(models.Model):
user = models.ForeignKey(User)
title = models.CharField(max_length=250, )
explain = models.CharField(max_length=500, )
assistance = models.CharField(max_length=250, choices=ASSISTANCE_CHOICES)
days = # MultipleChoiceField
created_date = models.DateTimeField( auto_now_add=True)
# Forms.py
class ObjectsForm(ModelForm):
objects = forms.MultipleChoiceField(widget=forms.CheckboxSelectMultiple,
choices=OBJECT_CHOICES)
class Meta:
model = ObjectModel
fields = '__all__'
# Views.py
def add_corporation(request)
if request.method == "POST":
form = ObjectsForm(request.POST)
if form.is_valid():
new_corporation = form.save(commit=False)
new_corporation.days = ','.join(form.cleaned_data['days'])
new_corporation.save()
return redirect('administrator:view_admin_user_corporation')
else:
form = ObjectsForm()
template = 'corporation.html'
context = {'form': form, }
return render(request, template, context)
当我点击提交按钮时(例如 select 对象 1 和 2),我得到这个错误:
Select a valid choice. ['1', '2'] is not one of the available choices.-
提示: 每个用户向我们发送请求,其中包含他可以做某事的解释和天数。因此,每个用户都可以 select 他想要的任何一天。
如果您必须将 CorporationRequest.days
保留为 CharField
,那么您可以将日期选择保存为字段中的逗号分隔字符串
class CorporationRequest(models.Model):
days = models.CharField(max_length=255)
您必须清理来自多选字段的数据,以便将其保存在该字段中
class CorporationRequestForm(ModelForm):
days = forms.MultipleChoiceField(widget=forms.CheckboxSelectMultiple,
choices=DAY_CHOICES)
class Meta:
model = CorporationRequest
fields = '__all__'
def clean_days(self):
return ','.join(self.cleaned_data['days'])
这意味着 CorporationRequest.days
现在是一个字符串而不是日期列表。您可以将 CorporationRequest
模型的方法添加到 return 天数列表
def get_days(self):
return self.days.split(',')