我正在尝试从 Django 3.0 中的一个表单中编写一个干净的方法。在这里,当用户选择 production_process 时,干净的方法没有任何价值。并且在我的模板页面中出现错误,如
Production process: Select a valid choice. That choice is not one of the available choices.
这是我的表格.py
class ProductionCapacityForm(forms.ModelForm):
date = forms.DateField(required=True, widget=forms.DateInput(attrs=
{
'class':'datepicker'
}))
def __init__(self, *args, **kwargs):
self.client = kwargs.pop('client')
self.request = kwargs.pop('request')
try:
self.work_time = kwargs.pop('work_time')
except:
pass
super(ProductionCapacityForm, self).__init__(*args, **kwargs)
self.fields['production_process'].queryset = ProductionProcess.objects.filter(is_deleted=False).values_list("name", flat=True).distinct()
self.fields['date'].widget.attrs\
.update({
'placeholder': 'Select Date',
'class': 'input-class_name'
})
def clean(self):
production_process = self.cleaned_data.get('production_process')
number_of_people = self.cleaned_data.get('number_of_people')
datee = self.cleaned_data.get('date')
pk = self.instance.pk
if production_process:
try:
if production_process not in ProductionCapacity.objects.get(client=self.client, date=datee,production_process=production_process.id):
self.cleaned_data['production_process'] = get_object_or_404(ProductionProcess,client=self.client,id=production_process.id)
except:
if ProductionCapacity.objects.filter(client=self.client, date=datee, production_process=production_process.id).exists():
raise forms.ValidationError('Production Process is already exists')
self.cleaned_data['number_of_people'] = number_of_people
self.cleaned_data['date'] = datee
return super(ProductionCapacityForm, self).clean()
class Meta:
model = ProductionCapacity
widgets = {
"date": McamDateInput1,
}
fields = ['date', 'production_process', 'number_of_people']
这是我的模型.py
class ProductionCapacity(models.Model):
client = models.ForeignKey(Client,on_delete=models.CASCADE)
date = models.DateField(blank=True, null=True)
production_process = models.ForeignKey(ProductionProcess,on_delete=models.CASCADE,blank=True,null=True)
number_of_people = models.IntegerField(blank=True, null=True)
这里
我怀疑是因为这条线:
self.cleaned_data['production_process'] = get_object_or_404(ProductionProcess,client=self.client,id=production_process.id)
get_object_or_404
用于检索要显示的对象。在这种情况下,您仅在代码中检索它。您不想要 404 http 响应,这几乎肯定不是有效的生产过程。
只要得到对象,如果它不存在,你就会得到
None
我对
clean()
方法做了一个小修改,以解决您遇到的 production_proces
字段未包含在表单的 cleaned_data
字典中的问题。
试试这个:
def clean(self):
cleaned_data = super().clean()
production_process = cleaned_data.get('production_process')
number_of_people = cleaned_data.get('number_of_people')
datee = cleaned_data.get('date')
pk = self.instance.pk
if production_process:
try:
if production_process.id not in ProductionCapacity.objects.filter(client=self.client, date=datee, production_process=production_process).values_list('production_process__id', flat=True):
cleaned_data['production_process'] = get_object_or_404(ProductionProcess, client=self.client, id=production_process.id)
except:
if ProductionCapacity.objects.filter(client=self.client, date=datee, production_process=production_process).exists():
raise forms.ValidationError('Production Process is already exists')
cleaned_data['number_of_people'] = number_of_people
cleaned_data['date'] = datee
return cleaned_data
这里的主要变化是我现在调用
super().clean()
来获取表单的cleaned_data
字典,而不是直接返回它。然后我用 number_of_people
和 datee
的清理值更新这本字典。最后,我要返回更新后的cleaned_data
字典。这样可以确保所有清理过的字段都包含在cleaned_data
字典中,包括production_process
.