For context, I’ve essentially got a form whereby the user fills in job details and can choose who was on the job from an existing list of workers and input their hours, rates, and total wages. I’ve implemented a script so the user can ‘add’ more workers to the form. The main issue is that the data is not saving specifically for the hours and rates field even though the wages field is, so I cannot submit the whole form. Given the JS is client-side it is more likely a django problem, I’ve tested it anyway by getting rid of my script.
Wage Model:
class EmployeeWage(models.Model):
job = models.ForeignKey(Operating_Budget, on_delete=models.CASCADE, null=True, blank=True)
worker = models.ForeignKey(EmployeeRecord, on_delete=models.CASCADE, null=True, blank=True)
wages = models.DecimalField(max_digits=10, decimal_places=2)
hours = models.DecimalField(max_digits=10, decimal_places=2, default=0)
rates = models.DecimalField(max_digits=10, decimal_places=2,default=0)
This is my Wage form:
class WorkerWagesForm(forms.ModelForm):
worker = forms.ModelChoiceField(queryset=EmployeeRecord.objects.all(), label="")
wages = forms.DecimalField(label="", max_digits=10, decimal_places=2)
hours = forms.DecimalField(label="", max_digits=10, decimal_places=2)
rates = forms.DecimalField(label="", max_digits=10, decimal_places=2)
class Meta:
model = EmployeeWage
fields = ('worker', 'wages', 'hours', 'rates')
class JobWorkerWageFormSetBase(BaseInlineFormSet):
def clean(self):
super().clean()
for form in self.forms:
if form.cleaned_data.get('hours') is None:
form.add_error('hours', 'Hours are required.')
if form.cleaned_data.get('rates') is None:
form.add_error('rates', 'Rates are required.')
WorkerWagesFormSet = inlineformset_factory(Operating_Budget, EmployeeWage, formset = JobWorkerWageFormSetBase, fields=('worker', 'wages', 'hours', 'rates'), extra=1, can_delete=True)
And view:
def addrev(request):
if request.method == 'POST':
print(request.POST)
job_form = Revenue(request.POST)
formset = WorkerWagesFormSet(request.POST, request.FILES)
print(formset.data)
if job_form.is_valid() and formset.is_valid():
print("Job Form cleaned data:", job_form.cleaned_data)
job = job_form.save()
job_worker_wages = formset.save(commit=False)
for job_worker_wage in job_worker_wages:
job_worker_wage.job = job
job_worker_wage.save()
worker = job_worker_wage.worker
if worker:
worker.job_completed += 1
worker.save()
print (worker)
return redirect('revenue')
else:
print(formset.errors)
print(job_form.errors)
else:
job_form = Revenue()
formset = WorkerWagesFormSet()
return render(request, 'addrev.html', {
'job_form': job_form,
'formset': formset,
})
This is the error message:
[{'hours': ['This field is required.', 'Hours are required.'], 'rates': ['This field is required.', 'Rates are required.']}]
I’ve checked multiple times to see if the field type in the models and forms were different which i couldn’t find any differences. All other inputs were valid.
Any help would be appreciated! Thank you
TargetToo is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.