繁体   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