[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 18:28:38 CEST 2012


Hey Jep,

If you don't get it figured out, feel free to share some code and I can
probably give you a point in the right direction.

On Wed, Oct 24, 2012 at 11:11 AM, Kurtis Mullins
<kurtis.mullins at gmail.com>wrote:

> Sorry, I should have been more clear. I believe you only need one save()
> before you call add() on the m2m field. I don't think you need to call it
> again afterwards.
>
> And yeah, you can call save with commit=False to get an object instance
> but I don't believe you will have a primary key until it's committed
> (saved).
>
> I could be wrong though :) my brain has been all over the place
> coding-wise lately.
> On Oct 24, 2012 11:06 AM, <jep200404 at columbus.rr.com> wrote:
>
>> 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
>>
>> _______________________________________________
>> 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/7d790c22/attachment-0001.html>


More information about the CentralOH mailing list