Saturday, May 29, 2010

Re: Get pk before commit

Did you really mean for commit=FALSE or pre_save signal???
Praveen

On May 29, 5:22 pm, Euan Goddard <euan.godd...@2degreesnetwork.com>
wrote:
> Hi,
>
> Could you explain the situation in a little more detail as I don't
> quite follow what you mean. As far as I'm aware if you have something
> like the following:
>
> >>> my_inst = MyModel(foo="bar", ...)
> >>> my_inst.save()
>
> You'll have the pk at this point even if the transaction hasn't been
> committed (providing you haven't specified a custom primary key
> field). So you should be able to do:
>
> >>> my_pk = my_inst.pk
>
> and use this for the other field that you need to set. If the other
> field is a foreign key to my_inst, then you should just be able to do:
>
> >>> other_model_inst.related_object = my_inst
> >>> other_model_inst.save()
>
> I'm not sure how this is affected by transactions. Unless you have a
> really good reason to, I'd let Django handle the transaction
> management for you, then you don't need to worry about save points,
> committing and rolling back.
>
> Euan
>
> On 29 May, 07:56, TheIvIaxx <theivi...@gmail.com> wrote:
>
> > Hello, I am trying to figure out how to get a pk in the middle of a
> > transaction. I need to set a field in the model based on the
> > ID(default pk) to be given.  As far as a i know, this ID should be
> > allocated during the transaction.  So i would imagine it would do the
> > INSERT, then i could get the pk and do what i need to do, then proceed
> > with the commit.
>
> > Is this possible?
>
> > Thanks

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment