任何帮助都将不胜感激,因为我正在用这根头发拔头发!我有一个Formview,它发送了repair_id和part_id到ModelForm。 ModelForm将此数据用作ModelChoiceField的输入,以提取一组值。我将ModelChoiceField子类化为PartMatModelChoiceField,以连接标签中的两列。这一切似乎都可以正常工作,但是每次我提交表单时,这些东西都不会提交为有效。
如果我查看html输出,则表单值/标签正确显示。如果我从ModelChoiceField中获取一个值,然后通过外壳提交表单,那么一切将显示为有效且没有错误。
测试一段时间后,我可以看到问题出在part_mat_nr字段上。
如果我将其切换回ModelChoiceField(无子类),则遇到相同的问题 如果我将查询集简化为PrdTtBom.objects.filter(item_no__iexact = part_id),我也会遇到同样的问题 如果我用静态设置的part_id替换了简化查询集中的变量part_id。有用。
此变量正在正确构建列表中,因此无法确定。
Model.py(相关字段)
part_mat_nr = models.CharField(db_column='Part_Mat_Nr', max_length=255, verbose_name='Part Number')
Forms.py
class RepairPartsTestForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
key = kwargs.pop('pknum', None)
part_id = kwargs.pop('part_id', None)
repair_id = kwargs.pop('repair_id', None)
total = kwargs.pop('total', None)
itembom = PrdTtBom.objects.filter(item_no__iexact=part_id).values('comp_item_no')
super(RepairPartsTestForm, self).__init__(*args, **kwargs)
self.fields['part_mat_nr'] = PartMatModelChoiceField(queryset=PrdTtCostsSqdx.objects.filter(matnr__in=itembom), to_field_name='matnr', label='Select Related Part Number', required=False)
self.fields['failcode'] = FailCodeModelChoiceField(queryset=Failcodes.objects.all(), label='Fail Code')
self.fields['repaired_fg'] = forms.ChoiceField(choices=((0, 'No'), (1, 'Yes')), label='Repaired:')
self.fields['faildesc'].widget = forms.HiddenInput()
self.fields['part_mat_desc'].widget = forms.HiddenInput()
self.fields['repairno'].widget = forms.HiddenInput()
self.fields['repairno'].initial = repair_id
self.fields['part_id'] = forms.CharField(initial=part_id, required=False, widget=forms.HiddenInput())
self.fields['repair_id'] = forms.CharField(initial=repair_id, required=False, widget=forms.HiddenInput())
self.fields['total'] = forms.CharField(initial=total, required=False, widget=forms.HiddenInput())
self.fields['key'] = forms.CharField(initial=key, required=False, widget=forms.HiddenInput())
def clean(self):
failcode = self.cleaned_data.get('failcode')
if failcode:
faildesc = Failcodes.objects.values_list('problem_desc', flat=True).get(problem_code=failcode)
else:
faildesc = None
self.cleaned_data['faildesc'] = faildesc
return self.cleaned_data
class Meta:
model = Repairparts
fields = '__all__'
views.py
class RepairPartsCreate(LoginRequiredMixin, GroupRequiredMixin, FormView):
title = 'Repair Parts: Create'
model = Repairparts
group_required = [u'AD_Group']
template_name = 'post_form.html'
success_url = 'user_rcf:summary'
form_class = RepairPartsForm
success_msg = 'Part Created Successfully!'
def get_context_data(self, **kwargs):
context = super(RepairPartsCreate, self).get_context_data(**kwargs)
context['title'] = self.title
return context
def post(self, request, *args, **kwargs):
context = super(RepairPartsCreate, self).get_context_data(**kwargs)
form = self.form_class(request.POST)
if form.is_valid():
data = form.cleaned_data
repair_id = data['repairno']
finalcharge = data['finalcharge']
Deliveryser.objects.filter(repairno__iexact=repair_id).update(finalcharge=finalcharge)
form.save(data)
context['success'] = True
messages.add_message(self.request, messages.SUCCESS, 'Part added successfully.')
return redirect(self.success_url, q=repair_id)
else:
repair_id = request.POST['repair_id']
part_id = request.POST['part_id']
total = 'partadd'
form = self.form_class(repair_id=repair_id,part_id=part_id,total=total)
context['repair_id'] = repair_id
context['part_id'] = part_id
context['title'] = self.title
context['form'] = form
return render(request, self.template_name, context)
return context