[Answer]-Django gives IntergrityError, user.id NOT null, don't know why


Since you have not included user in the list of fields, it will not be passed on to the ModelForm’s instance constructor.

Thus, what you want here is to change the user attribute of the form’s instance, not the attribute on the form object itself. In .save(), change self.user to self.instance.user. That should get you going.

However, I also want to suggest that this mechanism it generally an anti-pattern: why are you not allowing user to be a field on the ModelForm if it’s a required field that you populate anyway?


Leave a comment