Как использовать django TemporaryUploadedFile несколько раз?

Я пытаюсь создать несколько записей в django из объекта TemporaryUploadedFile, из-за его природы, когда я хочу создать больше записей, я получаю ошибку [Errno 2] Нет такого файла или каталога: '/tmp/tmpoz8_c7i3.upload. jpg'.

Я уже пытался переопределить классы, чтобы установить delete=False для tempfile.NamedTemporaryFile, но все равно получаю ошибку.

Версия Django = 4.1.3 Версия Python = 3.8.18

from django.core.files.uploadhandler import FileUploadHandler, TemporaryFileUploadHandler
from django.core.files.uploadedfile import UploadedFile
import os
from django.conf import settings
from django.core.files import temp as tempfile
class CustomTemporaryFileUploadHandler(TemporaryFileUploadHandler):
    """
    Upload handler that streams data into a temporary file.
    """

    def new_file(self, *args, **kwargs):
        """
        Create the file object to append to as data is coming in.
        """
        super().new_file(*args, **kwargs)
        self.file = CustomTemporaryUploadedFile(
            self.file_name, self.content_type, 0, self.charset, self.content_type_extra
        )

    def receive_data_chunk(self, raw_data, start):
        self.file.write(raw_data)

    def file_complete(self, file_size):
        self.file.seek(0)
        self.file.size = file_size
        return self.file

    def upload_interrupted(self):
        if hasattr(self, "file"):
            temp_location = self.file.temporary_file_path()
            try:
                self.file.close()
                os.remove(temp_location)
            except FileNotFoundError:
                pass


class CustomTemporaryUploadedFile(UploadedFile):
    """
    A file uploaded to a temporary location (i.e. stream-to-disk).
    """

    def __init__(self, name, content_type, size, charset, content_type_extra=None):
        _, ext = os.path.splitext(name)
        file = tempfile.NamedTemporaryFile(
            suffix=".upload" + ext,
            dir=settings.FILE_UPLOAD_TEMP_DIR,
            delete=False
        )
        super().__init__(file, name, content_type, size, charset, content_type_extra)

    def temporary_file_path(self):
        """Return the full path of this file."""
        return self.file.name

    def close(self):
        try:
            return self.file.close()
        except FileNotFoundError:
            # The file was moved or deleted before the tempfile could unlink
            # it. Still sets self.file.close_called and calls
            # self.file.file.close() before the exception.
            pass
Вернуться на верх