Django FileField.save() 生成重复文件
我有用户提交的内容,我试图将其写入文件,然后保存到 FileField
。
所以我有一个看起来像这样的模型:
class Revision(models.Model):
def custom_revision_file_path(instance, filename):
return '/'.join(['content/revisions', filename])
path = models.FileField(upload_to=custom_revision_file_path)
document = models.ForeignKey(Document)
...
并且创建实例的视图看起来像这样:
def handle_revisions(request):
document = Document.objects.get(id=request.GET['docid'])
basename = os.path.basename(str(document.path))
revision = Revision.objects.create(
document = document,
)
revision.path.save(basename, ContentFile(request.GET['revision']))
这一切都工作得相对很好,但有两个问题:
1) >ContentFile 在字符串中的每个字母之间放置一个空格,因此 'test'
变成 'tes t'
;
2)出于某种原因,每次我运行视图时,两个 Revision
实例都会以大致相同的路径保存。 IE。当第二个路径不应存在于全部。
什么给?
I have user submitted content that I am trying to write to a file and then save to a FileField
.
so I have a model that looks like this:
class Revision(models.Model):
def custom_revision_file_path(instance, filename):
return '/'.join(['content/revisions', filename])
path = models.FileField(upload_to=custom_revision_file_path)
document = models.ForeignKey(Document)
...
and the view that creates the instance looks like this:
def handle_revisions(request):
document = Document.objects.get(id=request.GET['docid'])
basename = os.path.basename(str(document.path))
revision = Revision.objects.create(
document = document,
)
revision.path.save(basename, ContentFile(request.GET['revision']))
This all works relatively fine but for two problems:
1) the ContentFile puts a space between each letter in my string so 'test'
turns into 't e s t'
;
2) for some reason each time I run the view two Revision
instances are saved with roughly the same path. ie. one path will be 'content/revisions/test.txt'
and the other will be 'content/revisions/test_1.txt'
when the second one shouldn't exist at all.
What gives?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,您永远不应该使用类似的东西来创建路径:
但是:
您不应该知道您的应用程序是在类Unix上运行还是在Windows上运行(是的,有些人使用 Windows 作为网络服务器)。
另外,您不应该调用
FileField
属性path
,这与FilePathField
不明确。该字段是
NOT NULL
吗?因为在您的create()
语句中您没有提供。这应该会引发错误。我不明白:
你想达到什么目的?您确定要在文件中存储 GET 参数吗?
为了回答你的问题,默认情况下,Django 不承担覆盖文件系统上存在的文件的责任,这就是为什么它会自动将其存储为 唯一路径通过添加后缀。
如果这种行为不适合,请考虑编写自定义文件存储。
First of all, you should never use something like that to create a path :
but :
You are not supposed to know if your application runs on Unix-like or on Windows (yes, some people use Windows as webserver).
Also, you should not call your
FileField
attributepath
, this is ambiguous withFilePathField
.Is this field
NOT NULL
? Because in yourcreate()
statement you don't provide one. This should raise an Error.I don't get this :
What are you trying to achieve ? Are you sure you want to store a GET parameter in the file ?
To answer your question, by default, Django does not take the responsability to overwrite a file that exists on your filesystem, this is why it automatically store it with an unique path by adding a suffix.
If this behaviour does not fits, consider writing a custom file storage.