开发者

Django Custom File Storage system

开发者 https://www.devze.com 2023-02-09 01:18 出处:网络
I have a custom storage import os from django.core.files.storage import S开发者_开发问答torage class AlwaysOverwriteFileSystemStorage(Storage):

I have a custom storage

import os
from django.core.files.storage import S开发者_开发问答torage


class AlwaysOverwriteFileSystemStorage(Storage):
    def get_available_name(self, name):
        """
        Directly Returns a filename that's 
        from what user input.
        """
        if self.exists(name):
    # Remove the existing file
        os.remove(name)
    # Return the input name as output
        return name

I want to know where should I put his AlwaysOverwriteFileSystemStorage.py file and how should by settings.py define DEFAULT_FILE_STORAGE

My Django root folder is /home/username/webapp

When I put DEFAULT_FILE_STORAGE = 'site.storage.AlwaysOverwriteFileSystemStorage', it returns an

Error

Error importing storage module site.storage: "No module named storage"

I am not familiar with Python/Django, any help will be much appreciated. Thank you.


You don't need to put anything in your settings.py. Just use it directly in your model. For example, create storage.py wherever your app is located and put OverwriteStorage() in it. Then, your model could look like this:

from storage import OverwriteStorage
...
class MyModel(models.Model):
    ...
    image = ImageField(upload_to='images', storage=OverwriteStorage())

I am also using a custom storage system to overwrite existing files. My storage.py looks like this:

from django.core.files.storage import FileSystemStorage

class OverwriteStorage(FileSystemStorage):
    """
    Returns same name for existing file and deletes existing file on save.
    """                                                              
    def _save(self, name, content):
        if self.exists(name):
            self.delete(name)
        return super(OverwriteStorage, self)._save(name, content)

    def get_available_name(self, name):
        return name


You can put it anywhere, but you should point the path to there in the settings.py

You can put this storage.py file in the root folder (the one that has manage.py) and point the path as storage.AlwaysOverwriteFileSystemStorage


I try to make this, but don`t work for me, until I make this on my storage.py :

import os
from django.conf import settings
from django.core.files.storage import FileSystemStorage


class MyFileSystemStorage(FileSystemStorage):
    def get_available_name(self, name, *args, **kwargs):
        if self.exists(name):
            os.remove(os.path.join(settings.MEDIA_ROOT, name))
        return super().get_available_name(name, *args, **kwargs)

No other changes was required.

Another way yo do this:

from django.core.files.storage import FileSystemStorage

class AvatarStorage(FileSystemStorage):

    def get_available_name(self, name, *args, **kwargs):
        self.delete(name)
        return super().get_available_name(name, *args, **kwargs)

The delete method check if the file exists. So these 3 line are enough. I hope this help somebody

0

精彩评论

暂无评论...
验证码 换一张
取 消