![](/img/trans.png)
[英]file upload - django admin - OSError - no such file or directory
[英]DJANGO - Upload an image file from directory to DB
我在我的項目文件夾中自動創建了圖像,我想將這些圖像上傳到我的 MySQL 數據庫。 這些圖像被命名為 img0.png、img1.png.. 等
我目前在做什么:
視圖.py
for i in range(imageCount):
img = open("img%s.png" %i)
obj = userImages(userId = userid, images=img)
obj.save()
模型.py
class userImages(models.Model):
userId = models.IntegerField()
images = models.ImageField()
這行不通。 我怎樣才能讓它工作?
數據庫不是放置文件的好地方,請考慮使用 s3 或雲。
如果您真的想將其保存在數據庫中,請使用BinaryField
但同樣的django 文檔警告您:
濫用 BinaryField
盡管您可能會考慮將文件存儲在數據庫中,但請考慮在 99% 的情況下這是糟糕的設計。 此字段不能替代正確的 static 文件處理。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.