如何在 Django 中手动分配图像字段 [英] how to manually assign imagefield in Django

查看:19
本文介绍了如何在 Django 中手动分配图像字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有 ImageField 的模型.如何手动为其分配图像文件?我希望它像对待任何其他上传的文件一样对待它...

I have a model that has an ImageField. How can I manually assign an imagefile to it? I want it to treat it like any other uploaded file...

推荐答案

查看 django 文档 django.core.files.File

See the django docs for django.core.files.File

其中 fd 是一个打开的文件对象:

Where fd is an open file object:

model_instance.image_field.save('filename.jpeg', fd.read(), True)

这篇关于如何在 Django 中手动分配图像字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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