1

I am adding a new field named "user" to the "order" model. I did make migrations. (Bonus question is: why in the sql3db column "user_id" was made, instead of "user"? But ok, I change form.fields['user'] to form.fields['user_id'], as the machine wants...)

I remove unneeded fields from the form in forms.py, and I try to add this fields in views.py (because I don't know how to send "user" to the forms.py, and I think it more secure like this).

And as I can understand - my new fields is absent for "order", when I use form.save(). This field can't be null by design.

screenshot

1
  • 1
    You should use user everywhere, not user_id. The "_id" suffix is used to access the raw value of the ForeignKey, usually the id of the related object, but you rarely need to access this Commented Oct 20, 2021 at 12:46

1 Answer 1

1

Your form has no user field, hence that will not work. What you can do is alter the object wrapped in the form with:

if form.is_valid():
    form.instance.user = request.user
    form.instance.email = request.user.email
    order = form.save()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.