您现在的位置是: 网站首页> 学习笔记> Django Django
django使用自定义存储类上传图片到阿里OSS,makemigrations时报异常
2024-07-31 [django] 1009人已围观
自定义了一个存储类,用于上传图片到阿里OSS,类如下:
from django.core.files.storage import Storage
from cloudstorage.alioss import alioss
import time
import logging
import uuid
from orderingshop.settings import ALI_OSS_ACCESS_DOMAIN
logger = logging.getLogger(__name__)
class OssStorage(Storage):
def __init__(self, upload_to: str = ""):
self.upload_to = upload_to
self.path_str = upload_to
if not upload_to.endswith("/"):
self.path_str = self.path_str + "/"
self.res = None
def save(self, name, content, max_length=None):
"""
文件保存
:param name: 上传时的文件名称,包含后缀名
:param content: 文件内容,File对象
:param max_length: 文件最大二进制长度
:return: 文件路径
"""
_date = time.strftime('%Y-%m-%d')
self.path_str = self.path_str + _date + '/' + '{}.{}'.format(uuid.uuid4(), name.split('.')[-1])
res = alioss.upload(self.path_str, content)
self.res = res
return self.path_str
def delete(self, name):
"""
删除文件
:param name: 相对路径文件名,此处并非上传时的文件名,而是在save()函数中返回的文件名
:return:
"""
try:
alioss.delete(name)
except Exception as e:
logger.error('删除文件失败,error: {}'.format(e))
def url(self, name):
"""
返回文件的url地址
:param name: 相对路径文件名,此处并非上传时的文件名,而是在save()函数中返回的文件名
:return:
"""
return f'{ALI_OSS_ACCESS_DOMAIN}{"" if ALI_OSS_ACCESS_DOMAIN.endswith("/") else "/"}{name}'
def path(self, name):
"""
返回文件的相对路径地址
:param name: 相对路径文件名,此处并非上传时的文件名,而是在save()函数中返回的文件名
:return: 相对路径地址
"""
return name
def open(self, name, mode='rb'):
"""
打开文件操作,一般pass即可
:param name: 相对路径文件名,此处并非上传时的文件名,而是在save()函数中返回的文件名
:param mode: 打开方式,默认'rb'
:return:
"""
pass
使用方式:
class MainImage(SiteBaseModel):
title = models.CharField(max_length=100, null=True, blank=True, verbose_name='标题')
url = models.ImageField(storage=OssStorage('xxxx/mainimage/'), verbose_name='图片')
class Meta:
verbose_name = '主图'
verbose_name_plural = verbose_name
执行makemigrations时抛异常:
Traceback (most recent call last): File "D:\codes\zzc\orderingshop\manage.py", line 22, in
main() File "D:\codes\zzc\orderingshop\manage.py", line 18, in main execute_from_command_line(sys.argv) File "D:\pyvenv\orderingshop\lib\site-packages\django\core\management__init__.py", line 442, in execute_from_command_line utility.execute() File "D:\pyvenv\orderingshop\lib\site-packages\django\core\management__init__.py", line 436, in execute self.fetch_command(subcommand).run_from_argv(self.argv) File "D:\pyvenv\orderingshop\lib\site-packages\django\core\management\base.py", line 412, in run_from_argv self.execute(args, cmd_options) File "D:\pyvenv\orderingshop\lib\site-packages\django\core\management\base.py", line 458, in execute output = self.handle(args, options) File "D:\pyvenv\orderingshop\lib\site-packages\django\core\management\base.py", line 106, in wrapper res = handle_func(*args, kwargs) File "D:\pyvenv\orderingshop\lib\site-packages\django\core\management\commands\makemigrations.py", line 259, in handle self.write_migration_files(changes) File "D:\pyvenv\orderingshop\lib\site-packages\django\core\management\commands\makemigrations.py", line 363, in write_migration_files migration_string = writer.as_string() File "D:\pyvenv\orderingshop\lib\site-packages\django\db\migrations\writer.py", line 141, in as_string operation_string, operation_imports = OperationWriter(operation).serialize() File "D:\pyvenv\orderingshop\lib\site-packages\django\db\migrations\writer.py", line 99, in serialize _write(arg_name, arg_value) File "D:\pyvenv\orderingshop\lib\site-packages\django\db\migrations\writer.py", line 63, in _write arg_string, arg_imports = MigrationWriter.serialize(_arg_value) File "D:\pyvenv\orderingshop\lib\site-packages\django\db\migrations\writer.py", line 282, in serialize return serializer_factory(value).serialize() File "D:\pyvenv\orderingshop\lib\site-packages\django\db\migrations\serializer.py", line 226, in serialize return self.serialize_deconstructed(path, args, kwargs) File "D:\pyvenv\orderingshop\lib\site-packages\django\db\migrations\serializer.py", line 91, in serialize_deconstructed arg_string, arg_imports = serializer_factory(arg).serialize() File "D:\pyvenv\orderingshop\lib\site-packages\django\db\migrations\serializer.py", line 391, in serializer_factory raise ValueError( ValueError: Cannot serialize: There are some values Django cannot serialize into migration files. For more, see https://docs.djangoproject.com/en/4.2/topics/migrations/#migration-serializing
解决方法:
在自定义存储类OssStorage中重写方法
def deconstruct(self):
# 返回字段名称、路径、位置参数和关键字参数
path = 'cloudstorage.storage.OssStorage'
kwargs = {
'upload_to': self.upload_to,
}
return (path, [], kwargs)
相关文章
- django在执行migrate时抛异常:django.db.utils.OperationalError: (1824, "Failed to open the referenced table 'auth_group'")
- django接入支付宝支付抛异常request sign failed. int() argument must be a string, a bytes-like object or a real number, not 'Sequence'
- django使用自定义存储类上传图片到阿里OSS,makemigrations时报异常
- django admin使用自定义用户类以后,新增用户保存的密码为明文
文章评论
暂无评论添加评论
点击排行
本栏推荐
标签云
热评文章
- django使用qq邮箱发送邮件
- mysql8设置数据库远程连接
- pip修改下载源为国内源
- win10看不到win7共享的文件夹的解决方法
- SQLyog连接 Mysql 8.0.11 报error no.1251- Client does not support authentic...
- 使用Oracel Net Nanager配置Oracle数据库远程访问
- 将anaconda的下载源切换为国内的源
- Python+selenium+firefox设置代理IP
- selenium+firefox+js实现动态设置firefox浏览器代理IP
- scrapy文件下载(高新技术企业认定网)
- Python调用JS代码
- Chrome浏览器的overrides的使用
站点信息
- 建站时间:2021-01-01
- 网站程序:Django 3.1.2
- 文章统计:53篇
- 文章评论:36条
- 统计数据: