Unable to make changes on DB or admin after crud operation./formset

I am building a mid-level project called School-Admin Panel with Django. I have completed most of the segments sucessfully, but I am having an issue with the performance forest.

The formset loads and displays correctly in the browser, but after I click submit, no changes are made in the DB nothing updates on the admin side. Please help me.

I am sharing portions of my code below:

forms.py

class PerformanceForm(forms.ModelForm):
    class Meta:
        model = Performance
        fields = ["student", "course", "marks", "grade"]
        widgets = {
            'student': forms.HiddenInput(),
            'course': forms.HiddenInput(),
        }

PerformanceFormSet = modelformset_factory(
    Performance,
    form=PerformanceForm,
    extra=0,       # only existing performances
    can_delete=False
)

views.py

def edit_performance(request):
    queryset = Performance.objects.all()  # optionally filter by course

    if request.method == "POST":
        formset = PerformanceFormSet(request.POST, queryset=queryset)
        if formset.is_valid():
            formset.save()
            return redirect("edit_performance")
        else:
            # Print errors for debugging
            print(formset.errors)
    else:
        formset = PerformanceFormSet(queryset=queryset)

    return render(request, "students/performance_edit.html", {"formset": formset})

template (performance_edit.html)

<h1>Edit Performance</h1>

<form method="POST">
    {% csrf_token %}
    {{ formset.management_form }}

    <table border="1" cellpadding="5">
        <thead>
            <tr>
                <th>Student</th>
                <th>Course</th>
                <th>Marks</th>
                <th>Grade</th>
            </tr>
        </thead>
        <tbody>
            {% for form in formset %}
                <tr>
                    <!-- Display student/course names -->
                    <td>{{ form.instance.student.name }}</td>
                    <td>{{ form.instance.course.name }}</td>

                    <!-- Editable fields -->
                    <td>{{ form.marks }}</td>
                    <td>{{ form.grade }}</td>

                    <!-- Hidden inputs -->
                    {{ form.student }}
                    {{ form.course }}
                </tr>
            {% endfor %}
        </tbody>
    </table>

    <button type="submit">Save Changes</button>
</form>

Hi Nicolas Riggs Nicolas,

The issue is most likely coming from the hidden fields (student and course) being required but not rendered with values, causing silent validation failures.

Because the fields are hidden, Django will still validate them — and if they are missing, blank, or altered, the formset will fail validation. The problem is that errors on hidden fields do not show up in your UI, so it looks like the form is submitting with no error while in reality formset.is_valid() is returning False every time.

Could you please share your model class Performance , To check the model class field & attributes

Вернуться на верх