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)
本文来自博客园,作者:super_ip,转载请注明原文链接:https://www.cnblogs.com/superip/p/17248225.html