Skip to content

Instantly share code, notes, and snippets.

@hirokiky hirokiky/models.py

Last active Dec 18, 2015
Embed
What would you like to do?
A customed FileSystemStorage of Django providing asynchronous deletion of files.
from django.db import models
from .storage import AsyncFileSystemStorage
a_fs = AsyncFileSystemStorage()
class UploadedFile(models.Model):
image = models.ImageField('Uploaded image file', upload_to='.', storage=a_fs)
description = models.TextField('Description of uploaded image file')
def delete(self, *args, **kwargs):
self.image.delete()
super(File, self).delete()
from django.core.files.storage import FileSystemStorage
from .tasks import async_file_delete
class AsyncFileSystemStorage(FileSystemStorage):
def delete(self, name):
file_path = self.path(name)
async_file_delete.delay(file_path)
import os
from celery import task
@task
def async_file_delete(file_path):
if os.path.exists(file_path):
try:
os.remove(file_path)
except OSError:
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.