你可以接收
pre_delete或
post_delete信号(请参见下面的@toto_tico的注释),并在FileField对象上调用delete()方法,因此(在models.py中):
class MyModel(models.Model): file = models.FileField() ...# Receive the pre_delete signal and delete the file associated with the model instance.from django.db.models.signals import pre_deletefrom django.dispatch.dispatcher import receiver@receiver(pre_delete, sender=MyModel)def mymodel_delete(sender, instance, **kwargs): # Pass false so FileField doesn't save the model. instance.file.delete(False)



