Django-admin后台文件上传相关功能

文件上传后,删除相关信息后,对应的上传文件也同时删除,具体代码可参考如下:

class AgreementList(models.Model):
    file=models.FileField(verbose_name="合同附件",upload_to='hetong/%Y/%m')
#删除文件相关库
from django.db.models.signals import pre_delete
from django.dispatch.dispatcher import receiver   
#可以删除信息时候自动删除文件
@receiver(pre_delete, sender=AgreementList)
def mymodel_delete(sender, instance, **kwargs):
    # Pass false so FileField doesn't save the model.
        instance.file.delete(False)

 

posted @ 2023-03-23 17:16  super_ip  阅读(40)  评论(0编辑  收藏  举报