使用 Django models.FileField 从内存中读取文件并在写入目录之前将其丢弃

Use Django models.FileField to read file from memory and discard it before it is written to the directory

我如何使用 Django 的 models.FileField 读取文件,处理内存中的数据,然后在 Django 尝试 save/write 将其放入我的目录之前将其丢弃。我想丢弃该文件,但仍保存模型的其他字段。我现在可以使用表单和视图来处理文件,但我想通过管理界面来完成,而不需要很多额外的逻辑

class DataField(models.Model):
    file = models.FileField()
    title = models.CharField()

    def save(self, *args, **kwargs):
        super(DataField, self).save(*args, **kwargs)

        some_background_task(self.file)
        # skip saving the file and avoid writing to directory, but save other fields
class DataField(models.Model):
    file = models.FileField(null=True)
    title = models.CharField()

    def save(self, *args, **kwargs):
        some_background_task(self.file)
        self.file = None

        super(DataField, self).save(*args, **kwargs)