last day (14 days later) » 

1:55 AM
0
Q: save() prohibited to prevent data loss due to unsaved related object

GodfreyI need to pass a primary key from a newly created modelform to another form field in the same view but i get an error. Any suggestions to make this work? It looks like in the past, this would be the answer: def contact_create(request): if request.method == 'POST': form = ContactForm(request....

 
A couple of things: 1) why are you setting model_instance.user to current_user.id instead of just current_user, 2) same with product.company = my_id instead of product.company = model_instance, and 3) what is the error you're getting (is it the ValueError you quoted?) and at which line in the code?
 
the error is always at product.save().
save() prohibited to prevent data loss due to unsaved related object 'company'. I applied your suggestions already.
 
I suggest using pdb or print statements to determine what my_id (or model_instance) are when saving product. It looks possible in that code to attempt saving products even if form.is_valid() is False.
 
At what point exactly is the model saved? I am actually connected to the psql server and i can confirm that the first form data is there with the primary key.
 
What happens if you get rid of this line: product.save(commit=False)?
 
1:55 AM
Same error. Data from first form in database.
 
And if you print out model_instance.pk just before product.save(), what does it show?
 
Just a minute.
i got 19 which is the primary key
on the company database
 
2:13 AM
My only remaining suggestion is putting import pdb ; pdb.set_trace() before the product.save() line and tracing through to the point of the error to see why the library code thinks it isn't saved.
 
Alright. I will try that.
 
Good luck. Let me know what you find... this is a curious bug. :-)
 
I will, i am having a hard time uploading my changed code to test. I am on a development server.
So pretty much, what is going on here?
i got a BdbQuit. No exception message supplied.
I think i give up for now. I will get back to it tomorrow. That error is persistent.
 

last day (14 days later) »