2👍
✅
Since you excluded the user, you need to get it back somehow, otherwise your rec_form.save()
would have exception because your model needs user
field to be filled.
The trick is to use save(commit=False)
. It will create an object in memory but it won’t save it. You just have to assign user
to the in-memory object and save it again:
if rec_form.is_valid():
new_rec = rec_form.save(commit=False)
new_rec.user = request.user
new_rec.save()
Check django doc about commit=False
in save()
method.
Source:stackexchange.com