[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 17:06:06 CEST 2012


On Wed, 24 Oct 2012 10:37:30 -0400, Kurtis Mullins <kurtis.mullins at gmail.com> wrote:

> You should only need 1 save. The .add() doesn't require a save if memory
> serves me correctly.

I am dealing with a newly created object/row that does not yet 
have a primary key (id) until the first save. 
How were you getting a value for the primary key for the .add() 
to use? Were you using an object/row that already existed and so 
had already been saved earlier? 

On Wed, 24 Oct 2012 10:29:03 -0400, jep200404 at columbus.rr.com wrote:

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

I might have discovered a solution. I see something now that 
I did not see when I first looked at the code[1]. 
Add commit=False to the first save. 

[1] busi = business.save(commit=False)
    from the last answer of 
    http://stackoverflow.com/questions/6090859/django-instance-needs-to-have-a-primary-key-value-before-a-many-to-many-relatio



More information about the CentralOH mailing list