How do you set the initial value for fileField for multistage form?

Hi folks,

I’m looking for some pointers on how to implement the following flow in django admin, and I think I need a bit of help in how I should do this, as I can’t see a reliable, easy way to do something I assumed was fairly straight forward.

What I want to do:

I want to provide a three step process, in the django admin for:

  1. uploading a CSV file
  2. processing the contents, and showing a preview of what will be done with the contents
  3. actually carrying out the bulk action, and showing the results

Where I am having a hard time

However, it’s not obvious to me what the best practice is for keeping a handle on the file uploaded at 1, when switching going from through the steps.

Here’s the first step - you’d upload a csv file, based on a sample (I might make this downloadable for ease of generation)

Next you see a preview of the upload - before you can confirm this submission

(please ignore the copy on the form - it’ll be tidied up and look nicer once I understand how to implement the actual functionality)

You’ll might notice form file upload widget has “no file selected” now.

I’d like to have it pre-populated, so it’s clear what file is being used for making these changes.

Then finally you see a confirmation, showing the newly updated values generated as a result of saving the form. I haven’t shown it, but it’s basically the table, but with some different copy saying stuff along the lines of “you have successfully updated X for Y, these are your options from here”.

I can’t see how to set an initial, or read-only value for a file upload, and have that persist to the next form submission.

It’s not obvious to me how I can set the initial value for a file upload here - and I was trying to avoid the complexity of creating a new model, just so I can keep track of the state of this upload in its own table in the database.

After reading around, I can see ways to make it look like I have a sample file here as outlined in this stackoverflow question:

What is the idiomatic, django-like approach here for multi-stage forms in admin?

It’s best to assume I don’t have a persistent file system, but I do have access to object storage, and the uploaded files would typically be less than 2mb in size.

I was considering storing the reference to an uploaded file in media storage somewhere, and uploading the file in the interim.

But if I can avoid all that extra complexity, I’d love to, as I’m just trying to store a bit of state between page loads, and had an idea in my head that you’d be able to store it in memory, or some ephemeral storage, server-side pretty easily.

You can see the ‘view’ code for the admin page here in this repo:

And the ‘urls’ here:

And the tests here:

I know the code is mess - this is a very much a WIP feature, so apologies in advance.

Keep in mind a basic principle of web architecture - HTTP is a “stateless” architecture.
The instance of the code that processes “request #1” may not be the same instance of the code that processes “request #2”. There’s no just “stashing stuff in memory” across requests, because it might just be a different process handling subsequent requests. (That’s why things like caches, session, etc are either stored in the database or in external processes such as redis or memcached - they are common repositories available across all instances of your web application servers.)

If I had to satisfy these requirements, I’d put the uploaded file in its “final” destination associated with whatever model is going to track it - but I would also have some type of “status” field in that model identifying its current “stage” within the process. (If nothing else, it allows this sequence to be resumed should something happen to interrupt that process.)
The data needs to go somewhere - why deal with moving it to multiple locations?