[CentralOH] (Django) ValueError: 'Book' instance needs to have a primary key value before a many-to-many relationship can be used.

jep200404 at columbus.rr.com jep200404 at columbus.rr.com
Wed Oct 24 16:29:03 CEST 2012


I have already solved the error in the subject line. 
Now, I am looking for a better solution. 

I have been using a ManyToManyField in Django with PostgreSQL 
for the first time and discovered new requirements it has. 
I solved the following error: 

   ValueError: 'QcBook' instance needs to have a primary key value before a many-to-many relationship can be used.

by doing a .save() to flesh out the primary key value, 
then doing the .add() for the ManyToManyField, 
then doing another .save(). 

I'm a bit uncomfortable that between the .save()s, 
an incomplete record is in the database. 
Is there a way to get the primary key value without .save()ing
so that I could have just one .save(), or am I stuck with the 
two .save()s and should I use a transaction around the two 
.saves()s? 
I _do_ have to worry about other processes adding to the 
table at the same time. 



More information about the CentralOH mailing list