# Django删除Model时自动删除文件

Note that when a model is deleted, related files are not deleted. If you need to cleanup orphaned files, you’ll need to handle it yourself (for instance, with a custom management command that can be run manually or scheduled to run periodically via e.g. cron).

## Django REST Framework


class MyModelViewSet(ModelViewSet):
queryset = MyModel.objects.all().order_by('-date_created')
serializer_class = MyModelSerializer

def perform_destroy(self, instance):
instance.file.delete(save=False)
instance.delete()


## 利用signals

from django.db.models.signals import pre_delete