[Answered ]-Formset saving any decimal except 0 even though formset is saved and is_valid() passes

1πŸ‘

βœ…

In your method save() of BaseFormSetValidation you are checking for value:

if form.cleaned_data.get(day):

The value 0 is falsy, so the if condition will be False and the conditional will not run.

None, 0, False are falsy values.

Instead, you can explicitly check for None:

class BaseFormSetValidation(BaseModelFormSet):

    ...

    def save(self, commit=True):
        super(BaseFormSetValidation, self).save(commit)
        from isoweek import Week
        for form in self.forms:
            week = Week(form.instance.year, form.instance.week)
            for day in DAYS:
                if form.cleaned_data.get(day) is not None:
                    date = week.day(int(day[-1]) - 1)
    
                    TimesheetEntry.objects.update_or_create(
                        timesheet=form.instance,
                        date=date,
                        defaults={'hours': form.cleaned_data.get(day)}
                    )
πŸ‘€NKSM

Leave a comment