Any help would be greatly appreciated as I am pulling my hair out with this one! I have a Formview that sends over a repair_id and a part_id to a ModelForm. The ModelForm takes this data as input for a ModelChoiceField pulling a set of values. I subclassed the ModelChoiceField as PartMatModelChoiceField in order to concatenate two columns in the label. This all appears to work fine but every time I submit the form things do not submit as valid.
If I look at the html output the form values / labels are showing up correctly. If I take one of the values from the ModelChoiceField and submit the form through the shell things show up as valid with no errors.
After testing for a while I can see the problem is with the part_mat_nr field.
If I switch it back to a ModelChoiceField (no subclass) I have the same problem If I simplify the queryset down to PrdTtBom.objects.filter(item_no__iexact=part_id) I have the same problem If I replace the variable part_id in the simplified queryset with the part_id staticly set. It works.
This variable is going through as it is building the list correctly so not sure whats up.
Model.py (relevant field)
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