我可以从 django 表单中pickle上传文件吗? 我的意思是 InMemoryUploadedFile
我有一个 django 表单,其中有一个接受用户简历的 FileField。 我稍后会将简历转换为 html 文档。 所以我想到立即对原始文档进行pickle并在其中存储一个数据库列,然后将其unpickle并转换它。 那可能吗?
I have a django form where I have a FileField which accepts user's resume. I am gonna convert the resume to a html document LATER. So I thought of pickling the original document right away and store in it a db colum and later unpickle it and convert it. Is that possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
最好只存储文件,然后在需要转换时再次打开它。 对其进行酸洗并存储在数据库中将对您的性能产生相当大的影响。 特别是当文件很大时。
it will be much better idea to just store the file and then open it again when you want to convert it. Pickling it and storing in the database will be a pretty big hit on your performance. Especially if the files are large.
我认为您不需要对 FileField 实例进行酸洗。 所有 FileField 存储都是驱动器上保存文件的路径...从这个意义上说,文件已经存储以供以后使用,并且对字段实例进行酸洗不会真正给你带来太多好处。 事实上,由于该字段实际上并不存储数据,因此对其进行酸洗并不会真正执行任何操作:-) 请参阅 FileField 上的 django 文档。
当您的带有 FileField 的模型被保存时,它会将文件路径保存到简历中。 稍后出现的任何进程都可以从驱动器加载简历,将其转换为 HTML,然后删除原始简历,或执行您喜欢的任何其他“清理”处理。
如果您需要将实际文件内容存储在数据库中,您将考虑为您的模型创建一个 Blob 字段...Blob 是特定于数据库的。 这个关于SO的问题有一个简单的实施。 但请注意,许多人认为在数据库中存储二进制文件是一个性能不佳的想法,实际上,您应该谨慎地以这种方式构建应用程序。 这篇 Google django-developers 论坛帖子 有一些很好的讨论, Django 和 blob 上的示例代码。
I don't think you'd need to resort to pickling the FileField instance. All FileField stores is the path on the drive where the file has been saved... in that sense, the file is already stored for later consumption, and pickling the field instance won't really get you much. In fact, since the field doesn't actually store the data, pickling it won't really do anything :-) See the django docs on FileField.
When your model with the FileField is saved, it will save the file path to the resume. Any process which later comes along can load the resume from the drive, convert it to HTML, and then either delete the original resume, or do whatever other "cleanup" processing you like.
If you need to store the actual file contents in the database, you would be looking at creating a Blob field for your model instead... blobs are DB-specific. This question on SO has a bare-bones implementation. Note, though, that many people feel storing binary in the DB is a poor-performing idea, and indeed you should be careful about structuring your app that way. This google django-developers forum post has some good discussion and sample code on Django and blobs.