views:

41

answers:

2

Hello,

i have an image saved on my HDD and i want to assign it to an ImageField, but i don't know how.

i've tried something like this: object.imagefield.save(path,File(open(path)))

But this makes an additional (invalid) copy of the image.

Can someone help me please?

thanks!

+1  A: 

I usually simply assign to the image field a string with the image's path, like this:

MyObject.image="/photo_path/photo_name.jpg"
MyObject.save()

I don't know if there is something wrong in doing that, but it always works.

dolma33
works great! thanks so much :)
amannn
+1  A: 

There might be a better way, but I do something like:

from django.core.files.base import ContentFile

srcFile = ContentFile( srcData ) # srcData is the contents of the local file
srcFile.open()
myObject.image.save(name=filename, content=srcFile)
ablerman