簡體   English   中英

如何使用 django 將圖像作為 BLOB 直接保存到 MySQL 數據庫中? 這些圖像是通過 django 管理面板動態上傳的?

[英]How do I save images directly into a MySQL database as a BLOB using django? These images are uploaded dynamically through the django admin panel?

What i want is that when i upload an image from the django admin panel, the image should be saved as a blob in MySQL database and not save the URL as django does when we use the imageField model. 發生了什么:通過管理面板上傳圖像-> url 存儲在數據庫中,圖像在文件夾中-> 通過模板中的 url 獲取的圖像我想要什么:通過管理面板上傳圖像-> 將圖像轉換為 blob-> 將 blob 存儲在數據庫中->直接從數據庫中獲取的圖像

在 Model 中創建 TextField 將文件編碼為 base64 並存儲在其中。

import base64

from django.db import models

class Blob(models.Model):

    file = models.TextField(
            db_column='data',
            blank=True)

    def set_data(self, data):
        self.file = base64.encodestring(data)

    def get_data(self):
        return base64.decodestring(self.file)

    data = property(get_data, set_data)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM