Django admin downloadable file processing

Here is what I want to do, and until I find something similar in my search. On my admin page, I have Filefield in my model. The remaining fields are read-only. I want to be able to download a file and process it immediately and extract information from it, so that these fields are read-only.

I was thinking of overriding the clean_ (modelfield) method for this FileField and doing this parsing and assigning stuff to it. But this is not done immediately after downloading the file, right? I thought this is done when the form / record is saved. Then I thought about adding a custom button to this administrative form called "process", which can be clicked after the file is uploaded. This will result in the assignment of values โ€‹โ€‹to read-only fields. But I canโ€™t decide what is the best approach to file processing and display updated fields on one page without unnecessary intervention.

Any thoughts? Thanks

+4
source share
1 answer

There are two solutions that I can think of with my limited knowledge. Since, by default, a file download will only start after the request is published, an alternative method needs to be developed.

1. Download the file through a script and process the file: Use a script (for example: a jQuery script ) to download the file, and after the download is complete, call the script (onComplete event) to display the values โ€‹โ€‹in a read-only field. This whole process can be associated with your "Process" button or with a trigger with a time delay after changing the FileField.

2 User form for uploading a file:. You can separate the file field and other fields (only the read fields that you mentioned). If you create a custom form only from the file upload field, and as soon as the user submits a request, you can display another form with the initial values โ€‹โ€‹displayed in read-only fields. This way you do not need to have a script, but you will need to have 2 forms.

Hope this helps. If you find any other solution, share it :)

+1
source

All Articles