Skip to content Skip to sidebar Skip to footer

Not Null Constraint Failed Django Createview

I would like to accomplish two goals on this project. First, I would like to save the logged-in user as the reviewer. Second, I want to pass the lawyer value from the foreignkey in

Solution 1:

You have a typo there, you are overriding form_valid but you have written post_valid.

defform_valid(self, form):
    review = form.save(Commit=False)
    review.lawyer = get_object_or_404(Lawyer, 
                                        slug=self.kwargs.get('slug'))
    review.reviewer = User.objects.get(user=self.request.user)

    # save and return HttpResponse
    review.save()
    return HttpResponseRedirect(self.get_success_url())

But, the super call will still call the parent class' form_valid which will still raise the error. I think you can save the review in this overriden method and return what the base class returns i.e. return HttpResponseRedirect(self.get_success_url())

Ref: form_valid in FormMixin class, form_valid in ModelFormMixin class

Post a Comment for "Not Null Constraint Failed Django Createview"