Django:存储图像数据库 [英] Django: Storing Images Database
本文介绍了Django:存储图像数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我以前使用的是默认数据库sqlite3,但今天我将其更改为postgresql。
Earlier I was using default database sqlite3, but today i've changed it to postgresql.
我想将图像文件保存在数据库中而不是项目目录本身中。我该怎么办?
I wants to save the image files in database not in project directory itself. How can I do that?
谢谢:)
推荐答案
将图像存储在数据库中而不是 media
文件夹中不是一个好主意。但是您可以为此使用 BinaryField :
This is not a good idea to store an image in DB instead media
folder. But you can use BinaryField for this:
model.py
class MyModel(model.Model):
image = models.BinaryField(blank=True)
view .py
def image(request):
image_file = request.FILES['image_file'].file.read()
MyModel.objects.create(image=image_file)
这篇关于Django:存储图像数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文