通过django shell保存图像/文件 [英] Saving image/file through django shell

查看:168
本文介绍了通过django shell保存图像/文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过django shell保存图像文件。

I am trying to save an image file through django shell.

我的 model.py 是: / p>

My model.py is:

class user(models.Model):
    name=models.CharField(max_length=20)
    pic=models.ImageField()

Everyhing对管理员和表单都很好,但是我想使用shell保存图像

Everyhing is fine with admin and forms but I want to save image using shell:

>>>user1=User(name='abc', pic="what to write here")


推荐答案

from django.core.files import File

user1=User(name='abc')
user1.pic.save('abc.png', File(open('/tmp/pic.png', 'r')))

您将最终将图像 abc.png 复制到 upload_to 目录
ImageField 中指定。

You will end up with the image abc.png copied into the upload_to directory specified in the ImageField.

在这种情况下, user1.pic.save 方法也将保存 user1 实例。
可以在这里找到保存 ImageField 的文档 https://docs.djangoproject.com/en/dev/ref/files/file/

In this case, the user1.pic.save method will also save the user1 instance. The documentation for saving an ImageField can be found here https://docs.djangoproject.com/en/dev/ref/files/file/

这篇关于通过django shell保存图像/文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆