Search code examples
pythondjangofilefieldimagefield

quick question: how to associate a file with an ImageField in django?


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!


Solution

  • 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.