आपको अपने स्वयं के भंडारण को परिभाषित करना चाहिए, इसे फाइलसिस्टमस्टोरेज से विरासत में मिला, और ओवरराइड करें OS_OPEN_FLAGS वर्ग विशेषता और get_available_name()विधि को :
Django संस्करण: 3.1
परियोजना / कोर / फ़ाइलें / भंडार / बैकेंड / local.py
import os
from django.core.files.storage import FileSystemStorage
class OverwriteStorage(FileSystemStorage):
"""
FileSystemStorage subclass that allows overwrite the already existing
files.
Be careful using this class, as user-uploaded files will overwrite
already existing files.
"""
OS_OPEN_FLAGS = os.O_WRONLY | os.O_TRUNC | os.O_CREAT | getattr(os, 'O_BINARY', 0)
def get_available_name(self, name, max_length=None):
"""
This method will be called before starting the save process.
"""
return name
अपने मॉडल में, अपने कस्टम ओवरराइटसाइट का उपयोग करें
MyApp / models.py
from core.files.storages.backends.local import OverwriteStorage
class MyModel(model.Model):
my_file = model.FileField(storage=OverwriteStorage)
FileField। जब भीFileFieldसहेजा जाता है, फ़ाइल की एक नई प्रतिलिपि बनाई जाती है। इससे बचने के लिए विकल्प जोड़ना काफी सरल होगा।