Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django admin uploaded file processing

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

like image 765
gamadeus Avatar asked Nov 14 '22 00:11

gamadeus


1 Answers

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 :)

like image 165
Konstant Avatar answered Jan 02 '23 12:01

Konstant