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

Kurtis Mullins kurtis.mullins at gmail.com
Wed Oct 24 16:37:30 CEST 2012


Hey,

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

You could wrap this in a transaction. I believe django offers a transaction
middle ware to automatically rollback if errors were encountered.

Good luck!
On Oct 24, 2012 10:30 AM, <jep200404 at columbus.rr.com> wrote:

> 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.
>
> _______________________________________________
> CentralOH mailing list
> CentralOH at python.org
> http://mail.python.org/mailman/listinfo/centraloh
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/centraloh/attachments/20121024/eb56a738/attachment.html>


More information about the CentralOH mailing list