在 Django 中调用保存时如何停止创建新文件?

How to stop creation of new File when call save in django?

我正在使用 Django 模型,例如:

pickle_embedding_file=models.FileField(upload_to='pickle_embedding/',blank=True,null=True)

每当我调用以下函数时:

    def set_pickle_embeddingFile(self,dict):
    content = pickle.dumps(dict)
    fid = ContentFile(content)
    self.pickle_embedding_file.save(str(self.user_id)+"pickle.pickle",fid)
    fid.close()

正在创建一个新文件:

68b335fa-6838-4c11-bd2a-32526d9b112dpickle.pickle
68b335fa-6838-4c11-bd2a-32526d9b112dpickle_jHIn3B6.pickle
68b335fa-6838-4c11-bd2a-32526d9b112dpickle_KJ5zBG0.pickle
68b335fa-6838-4c11-bd2a-32526d9b112dpickle_PUepizZ.pickle
68b335fa-6838-4c11-bd2a-32526d9b112dpickle_TREbCvy.pickle

我想覆盖文件或想删除旧文件。我该怎么做?

创建新文件之前先删除以前的文件

self.pickle_embedding_file.delete()
self.pickle_embedding_file.save(str(self.user_id)+"pickle.pickle",fid)