django admin上传文件处理
这就是我想做的,到目前为止我在搜索中没有找到类似的东西。 在我的管理页面中,我的模型中有一个文件字段。其余字段都是只读的。 我希望能够上传文件并立即处理它,并从中提取信息以分配给这些只读字段。
我想重写这个 FileField 的 clean_(modelfield) 方法,并在其中进行解析和分配内容。但这并不是在文件上传后立即完成的,对吧?我认为这是在保存表单/条目时完成的。 接下来,我想到向这个名为“进程”的管理表单添加一个自定义按钮,可以在文件上传后单击该按钮。这将触发对只读字段的值分配。 但我无法决定处理文件并在一页中显示更新字段的最佳方法,而无需太多修改。
有什么想法吗?谢谢
Here's what I want to do, and I did not find something similar in my search so far.
In my admin page, I have a Filefield in my model. The rest of the fields are all read only.
I want to be able to upload a file and process it immediately and to extract info from it to assign to these read only fields.
I thought of overriding the clean_(modelfield) method for this FileField and do this parsing and assigning stuff in it. But this is not done right after the file is uploaded, right? I thought this is done when the form/entry is saved.
Next I thought of adding a custom button to this admin form called 'process' which can be clicked after the file is uploaded. This would trigger the assignment of values to the read only fields.
But I am not able to decide on what is the best approach to process the file and display the updated fields in one page without too much of tinkering.
Any thoughts? Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
以我有限的知识能想到的解决办法有两种。由于默认情况下,只有在发出请求后才会开始文件上传,因此需要设计一种替代方法。
1.通过脚本上传文件并处理文件:使用脚本(例如:JQuery 脚本) 上传文件,上传完成后,触发脚本(onComplete 事件)将值呈现到只读字段中。一旦文件字段发生更改,整个过程就可以与“处理”按钮或延时触发器相关联。
2 文件上传自定义表单:您可以分离文件字段和其他字段(您提到的只读字段)。如果您设计一个仅包含文件上传字段的自定义表单,并且一旦用户提交请求,您就可以呈现另一个表单,并在只读字段中呈现初始值。这样你就不需要任何脚本,但你必须有两种形式。
希望这有帮助。如果您找到任何其他解决方案,请分享:)
There are two solutions that I can think of with my limited knowledge. Since, by default, the file upload will only start once the request is posted, an alternative way needs to be designed.
1. Upload file via a script and process the file: Use a script (eg: JQuery script) to upload the file and once upload is complete, trigger a script (onComplete event) to render the values into read-only field. This entire process can be associated to your "Process" button or a time-delayed trigger once the FileField is changed.
2 Custom form for file upload: You can detach the file field and other fields (read only fields that you mentioned). If you design a custom form with just the file upload field and once the user submits the request, you can render another form with rendered initial values in the read only fields. That way you need not have any script but you will have to have 2 forms.
Hope this helps. If you find any other solution, do share it :)