[Fixed]-Save form as foreign key's object

1👍

When you first create a Gig by saving the form, there is no profile linked to it (because it wasn’t declared in your form). So, your error occurs because save_it.profile has no value.

I think you are trying to link a Gig to the Profile of the user logged in. To do this, I think you can do

save_it.profile = request.user.profile
save_it.save()

instead of assigning the user itself.

OneToOneField lets you reference objects in both directions. So you can do user.profile and profile.user.

👤zubhav

Leave a comment