Django初窥门径-自定义附件存储模型

前言

Django自带了一个名为FileField的字段,用于处理文件上传。然而,有时我们需要更多的控制权,例如定义文件的存储路径、文件名以及文件类型。在本篇文章中,我们将探讨如何自定义Django附件存储模型。

创建attachment应用

bash 复制代码
python manage.py startapp attachment

然后,在项目的settings.py文件中,将应用注册到INSTALLED_APPS列表中,如下所示:

py 复制代码
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'rest_framework',
    'drf_yasg2',
    'django_filters',
    'account.apps.AccountConfig',
    'oauth',
    'attachment'
]

创建模型

定义Attachment

py 复制代码
from django.db import models

# Create your models here.
from rest_framework.reverse import reverse

from CodeVoyager.mixins import BaseModelMixin
import uuid


class BlobField(models.Field):
    description = 'Blob'

    def db_type(self, connection):
        return 'mediumblob'


class Attachment(BaseModelMixin):
    file_id = models.UUIDField(auto_created=True, default=uuid.uuid4, editable=False)
    file_name = models.CharField('文件名', max_length=200, unique=True)
    mime_type = models.CharField('MIME类型', max_length=100)
    file_size = models.PositiveIntegerField('文件长度')
    blob = BlobField('文件内容')

    class Meta:
        verbose_name = '附件'
        verbose_name_plural = verbose_name

    def get_url(self, request):
        return reverse('attachment:download', request=request, kwargs={'attachment_id': self.file_id})
字段名称 类型 用途
file_id UUIDField 存储文件的唯一标识符
file_name CharField 存储文件的名称,即原始文件名
mime_type CharField 存储文件的MIME类型
file_size PositiveIntegerField 存储文件的大小(以字节为单位)
blob 自定义 BlobField 存储文件的二进制内容,即文件的实际数据

将更改应用到数据库

bash 复制代码
python manage.py makemigrations
python manage.py migrate

自定义Django存储

定义存储类

py 复制代码
#!/usr/bin/python
# -*- coding: utf-8 -*-

from django.core.files.base import ContentFile, File
from django.core.files.storage import Storage
from django.utils.deconstruct import deconstructible


@deconstructible
class AttachmentStorage(Storage):
    """附件存储"""

    def __init__(self, model=None):
        from .models import Attachment
        self.model = Attachment

    def _open(self, file_id, mode='rb'):
        instance = self.model.objects.get(file_id=file_id)
        file = ContentFile(instance.blob)
        file.filename = instance.file_name
        file.mimetype = instance.mime_type
        return file

    def _save(self, name, content: File):
        blob = content.read()
        mime_type = getattr(content, 'content_type', 'text/plain')
        self.model.objects.create(
            file_name=name,
            blob=blob,
            file_size=content.size,
            mime_type=mime_type
        )
        return name

    def exists(self, name):
        return self.model.objects.filter(file_name=name).exists()


attachment_storage = AttachmentStorage()
方法名称 参数 返回值 用途
_open file_id, mode='rb' ContentFile 打开文件以供读取,根据给定的file_idAttachment模型中获取文件记录并返回ContentFile对象。
_save name, content: File 文件名 保存文件,将文件名和文件内容作为参数,创建Attachment模型记录并将文件信息保存到数据库。
exists name 布尔值 (TrueFalse) 检查文件是否存在,根据给定的文件名查询Attachment模型,返回True如果文件存在,否则返回False

这些方法共同组成了AttachmentStorage类,用于处理附件文件的存储和访问。_open 方法用于读取文件,_save 方法用于保存文件,而exists 方法用于检查文件是否存在。请注意,初始化方法__init__接受一个model参数。

定义视图

上传视图

py 复制代码
class AttachmentUploadView(APIView):
    permission_classes = (permissions.IsAdminUser,)

    def post(self, request, version):
        try:
            file = request.FILES['file']
        except MultiValueDictKeyError:
            raise ValidationError('参数错误')
        name = attachment_storage.save(file.name, file)
        attachment = get_object_or_404(Attachment, file_name=name)
        return JsonResponse({'download_url': attachment.get_url(request)}, status=status.HTTP_201_CREATED)
  • 作用:处理附件的上传操作。
  • 功能 :当接收到POST请求时,该视图尝试从请求中获取名为 'file' 的文件,然后使用自定义的存储后端 attachment_storage 来保存文件。接着,它在数据库中查找与文件名匹配的附件记录,并返回包含下载链接的 JSON 响应。这个视图的主要目的是允许用户上传附件,并提供上传后的附件的下载链接。

下载视图

py 复制代码
class AttachmentDownloadView(APIView):
    permission_classes = (permissions.IsAuthenticated,)

    def get(self, request, version, attachment_id=None):
        attachment = attachment_storage.open(attachment_id)
        response = HttpResponse(attachment, content_type=attachment.mimetype)
        response['Content-Disposition'] = 'attachment;filename={name}'.format(name=attachment.filename).encode('utf-8')
        return response
  • 作用:处理附件的下载操作。
  • 功能:当接收到GET请求时,该视图使用传递的 attachment_id 参数来打开相应的附件。然后,它创建一个包含附件内容的 HTTP 响应对象,设置响应的内容类型为附件的 MIME 类型,并设置响应头 Content-Disposition,指定附件的文件名。最后,它返回包含附件内容的 HTTP 响应。这个视图的主要目的是允许用户通过提供附件的唯一标识符来下载附件。

Content-Disposition 是一个HTTP响应头,它用于指示浏览器如何处理接收到的文件。具体来说,Content-Disposition 头的值告诉浏览器应该如何处理响应的内容,通常用于文件下载操作。

注册视图

py 复制代码
#!/usr/bin/python
# -*- coding: utf-8 -*-
from django.urls import re_path, path

from .views import AttachmentUploadView, AttachmentDownloadView

app_name = 'attachment'

urlpatterns = [
    re_path(r'upload', AttachmentUploadView.as_view(), name='upload'),
    path(r'download/<uuid:attachment_id>', AttachmentDownloadView.as_view(), name='download'),
]

使用swagger测试接口

上传

下载

结语

在开发Web应用程序时,文件上传和下载是常见的功能之一,但同时也需要特别关注安全性。通过合理的安全性配置,你可以保护应用程序和用户的数据免受潜在的威胁。在实际的项目中,我们可以增加一些重要的安全性措施,包括文件类型验证、文件大小限制、CSRF保护、存储路径安全性和其他关键措施,以确保文件上传和下载功能的安全性。

相关推荐
权^38 分钟前
MySQL--聚合查询、联合查询、子查询、合并查询(上万字超详解!!!)
大数据·数据库·学习·mysql
Code成立1 小时前
1、深入理解Redis线程模型
数据库·redis·bootstrap
缘友一世3 小时前
macos安装mongodb
数据库·mongodb·macos
万事大吉CC4 小时前
mysql单表查询·3
数据库·mysql
bin91535 小时前
【EXCEL数据处理】000010 案列 EXCEL文本型和常规型转换。使用的软件是微软的Excel操作的。处理数据的目的是让数据更直观的显示出来,方便查看。
大数据·数据库·信息可视化·数据挖掘·数据分析·excel·数据可视化
Miqiuha5 小时前
lock_guard和unique_lock学习总结
java·数据库·学习
一 乐6 小时前
学籍管理平台|在线学籍管理平台系统|基于Springboot+VUE的在线学籍管理平台系统设计与实现(源码+数据库+文档)
java·数据库·vue.js·spring boot·后端·学习
Java探秘者10 小时前
Maven下载、安装与环境配置详解:从零开始搭建高效Java开发环境
java·开发语言·数据库·spring boot·spring cloud·maven·idea
2301_7869643610 小时前
3、练习常用的HBase Shell命令+HBase 常用的Java API 及应用实例
java·大数据·数据库·分布式·hbase
阿维的博客日记11 小时前
图文并茂解释水平分表,垂直分表,水平分库,垂直分库
数据库·分库分表