Skip to content

Instantly share code, notes, and snippets.

@fabiomontefuscolo
Created January 9, 2012 19:20
Show Gist options
  • Select an option

  • Save fabiomontefuscolo/1584462 to your computer and use it in GitHub Desktop.

Select an option

Save fabiomontefuscolo/1584462 to your computer and use it in GitHub Desktop.
Let the Django FileField overwrite files with the same name
# -*- coding: utf-8 -*-
from django.conf import settings
from django.core.files.storage import FileSystemStorage
from django.db import models
class OverwriteStorage(FileSystemStorage):
'''
Muda o comportamento padrão do Django e o faz sobrescrever arquivos de
mesmo nome que foram carregados pelo usuário ao invés de renomeá-los.
'''
def get_available_name(self, name):
if self.exists(name):
os.remove(os.path.join(settings.MEDIA_ROOT, name))
return name
class Media(models.Model):
name = models.CharField(u"Nome", max_length=128))
media = models.FileField(u"Arquivo", upload_to=settings.MEDIA_DIR, storage=OverwriteStorage())
@davidfischer-ch

Copy link
Copy Markdown

Thank you !

I splitted this is two parts into my own toolbox.

https://github.com/davidfischer-ch/pytoolbox/blob/master/pytoolbox/django/storage.py

@dotmobo

dotmobo commented Sep 28, 2016

Copy link
Copy Markdown

Very usefull thanks !

@billyeh

billyeh commented Sep 14, 2017

Copy link
Copy Markdown

Nice script. By the way, on line 13, os.remove(os.path.join(self.location, name)) is more accurate if the storage uses a different location than just the media root. Also now that get_available_name takes the max_length argument it would be better to return super(OverwriteStorage, self).get_available_name(name, max_length).

@AlwaysYou

Copy link
Copy Markdown

Thanks!!!

@vahedq

vahedq commented Apr 27, 2018

Copy link
Copy Markdown

Is there any suggestions to make this work with s3?

@hackedhead

Copy link
Copy Markdown

@vahedq The django-storages app has support for S3 and includes a setting for enabling file overwriting.

@sepulm01

Copy link
Copy Markdown

excelente!

@KimberH

KimberH commented Jun 14, 2020

Copy link
Copy Markdown

Newbie questions: How do I install this in Wordpress? Does it work with PHP 7+?

@fabiomontefuscolo

Copy link
Copy Markdown
Author

@KimberH that doesn't work on PHP. This 10 years old snippet is intended to run on Django framework, which is written in Python. I'm not sure on WordPress. Google for wp_handle_upload_overrides and check what you get.

@davidshumway

Copy link
Copy Markdown

@julianklotz

julianklotz commented Jan 20, 2022

Copy link
Copy Markdown

Thanks! Helped me fix my problem quickly.

Here’s a sidenote: The original get_available_name method e.g. does a check for suspicious file operations and max_length. To benefit from the original method, call super():

storage.py

from django.core.files.storage import FileSystemStorage


class FileSystemOverwriteStorage(FileSystemStorage):
    """
    Custom file system storage: Overwrite get_available_name to make Django replace files instead of
    creating new ones over and over again.
    """
    def get_available_name(self, name, max_length=None):
        self.delete(name)
        return super().get_available_name(name, max_length)

… and in settings.py:

# …
DEFAULT_FILE_STORAGE = '<my-app>.storage.FileSystemOverwriteStorage'

@kotrotko

Copy link
Copy Markdown

Until now, it is useful)) My (paid) Claude was not able to solve the same problem. Thanks a lot and glory to natural intelligence))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment