I’m having trouble with a Django application where form data gets submitted but never saves to the database. The setup works like this - users first select some criteria which generates a list of items, then they can modify values for those items and submit. I can see the POST data coming through correctly, but nothing gets stored in MySQL.
Here’s my model for tracking employee payroll data:
class PayrollRecord(models.Model):
period_month = models.IntegerField()
period_year = models.IntegerField()
company = models.ForeignKey(Company, on_delete=models.DO_NOTHING)
worker = models.ForeignKey(Worker, on_delete=models.CASCADE, null=True, default=None)
salary = models.IntegerField()
total_pay = models.IntegerField()
working_days = models.IntegerField()
attendance_days = models.IntegerField()
calc_a = models.DecimalField(max_digits=10, decimal_places=2)
calc_b = models.DecimalField(max_digits=10, decimal_places=2)
calc_difference = models.DecimalField(max_digits=10, decimal_places=2)
calc_final = models.DecimalField(max_digits=10, decimal_places=2)
def save(self, *args, **kwargs):
self.pk = f"{self.period_month}.{self.period_year}"
super().save(*args, **kwargs)
My view handles the form processing:
class CreatePayrollView(FormView):
template_name = 'create_payroll.html'
form_class = PayrollEntryForm
model = PayrollRecord
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
prev_month, current_year, days_count = get_month_info()
context['prev_month'] = prev_month
context['current_year'] = current_year
context['days_count'] = days_count
context['companies'] = Company.objects.all()
company_id = self.request.POST.get('company')
if company_id:
workers = fetch_company_workers(company_id)
context['workers'] = workers
return context
def form_valid(self, form):
period_month = form.cleaned_data['period_month']
period_year = form.cleaned_data['period_year']
company = form.cleaned_data['company']
workers = form.cleaned_data['worker']
salary = form.cleaned_data['salary']
total_pay = form.cleaned_data['total_pay']
working_days = form.cleaned_data['working_days']
attendance_days = form.cleaned_data['attendance_days']
for worker in workers:
PayrollRecord.objects.create(
period_month=period_month,
period_year=period_year,
company=company,
worker=worker,
salary=salary,
total_pay=total_pay,
working_days=working_days,
attendance_days=attendance_days
)
return redirect('worker_list')
The form definition:
class PayrollEntryForm(forms.ModelForm):
period_month = forms.IntegerField()
period_year = forms.IntegerField()
company = forms.ModelChoiceField(queryset=Company.objects.all(), required=True)
worker = forms.ModelMultipleChoiceField(queryset=Worker.objects.filter(active=True), required=True, widget=forms.CheckboxSelectMultiple)
salary = forms.IntegerField()
total_pay = forms.IntegerField()
working_days = forms.IntegerField()
attendance_days = forms.IntegerField()
class Meta:
model = PayrollRecord
fields = ('period_month', 'period_year', 'company', 'worker', 'salary', 'total_pay', 'working_days', 'attendance_days')
No errors show up in Django logs or MySQL logs. What could be preventing the data from saving?