Lets say for a model i have the following:

class Image(models.Model):
    image = models.ImageField()

>>> image = Image()
>>> # Somehow get ID
>>> destination = open('ID/name.jpg', 'wb+')
>>> for chunk in f.chunks():
...        destination.write(chunk)
>>> destination.close()
>>> image.image = "ID/name.jpg"
>>> image.save()

I'd like to save the image file into a folder based on the ID of the
new instance.  The only thing i can think of is to create the new
folder mid transaction.  I guess another way to do it would be to save
a new instance with a temp file or set it to null=True, then create
the folder, but that seems bad to me.

Thanks

On May 29, 8:30 am, Praveen <praveen.python.pl...@gmail.com> wrote:
> 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-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to