1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

DjangoでImageFieldに画像をプログラムから追加する

Posted at

DjangoのImagefieldに画像をフォームではなくプログラムから追加してみようとおもって詰まったのでメモ

用意するモデルはこんな感じ

class Picture(models.Model):
    image = models.ImageField(upload_to='hoge')

こいつに画像。例えばサーバー上に既にある画像とかを紐づける

from django.core.files import File

img = Picture()
img.image.save('file_name',File(open('file_path','rb')))

あんまよくわかってないけどとりあえずできたのでよし....
何か良くない部分があればご指摘していただけると嬉しいです

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?