[Answered ]-Prevent Django to save an specific form from a formset

1👍

Just save the forms that match:

if request.method == 'POST':
    formset = WeeksFormset(request.POST)
    if formset.is_valid():
        for form in formset.forms:
            instance = form.instance
            if any(
                f is not None
                for f in [
                    instance.monday,
                    instance.tuesday,
                    instance.wednesday,
                    instance.thursday,
                    instance.friday,
                    instance.saturday,
                    instance.sunday,
                ]
            ):
                instance.user = request.user
                form.save()
        # do *not* save the formset
        return redirect('time')

Leave a comment