Skip to content Skip to sidebar Skip to footer

How To Solve The Not Null Constraint Failed Error In Django

I am getting an IntegrityError when I want to save a new course on my e-learning website. Of course, I have searched for a similar solution on StackOverflow but I couldn't find an

Solution 1:

You need to set the user as the owner what currently is not happening. The form is being saved with the fields you have set. There are multiple ways of adding the user. You could do it this way.

from django.shortcuts import redirect

class CourseCreateView(OwnerCourseMixin, CreateView):
    permission_required = "courses_app.add_course"
    template_name = "courses_app/manage/course/form.html"
    success_url = reverse_lazy("manage_course_list")

    def form_valid(self, form):
        instance = form.save(commit=False)
        instance.owner = self.request.user
        instance.save()
        return redirect(self.get_success_url())

Post a Comment for "How To Solve The Not Null Constraint Failed Error In Django"