How to Upload and Process a File in Django Admin

Hello everyone,

I am working on a Django project where I need to upload a file via the Django admin interface, process it using a specific class, and then store the processed data in different models based on the file’s content. The key requirement is that the file itself should not be saved in the database or the file system after it is uploaded.

Here’s a summary of what I need:

  1. Upload a file (KML format) via Django Admin: I want to create a file upload field in the admin that allows users to upload a KML file.

2.Process the file using a custom class: After the file is uploaded, it should be immediately processed by a class I’ve written (KmlLicense), which extracts geometrical data from the KML file.

  1. Store processed data in other models: The extracted data (points, lines, polygons) should be stored in different models (LicensePoint, LicenseLine, LicensePolygon), depending on the geometry.

  2. No need to save the uploaded file: I don’t want the uploaded KML file itself to be saved either in the database or in the media directory of my Django project.

Questions:

How can I set up the Django admin to upload a file, process it with my class, and not save the file itself?
Is there a way to do this entirely within Django Admin without creating a temporary model?
It’s not necessary to provide exact code; a guideline would be enough since I couldn’t find anything related to this use case in the Django documentation.

Any example code or pointers would be greatly appreciated!

Thank you for your help!

I’d strongly recommend against trying it.

Quoting directly from the Django docs for the admin:

The admin has many hooks for customization, but beware of trying to use those hooks exclusively. If you need to provide a more process-centric interface that abstracts away the implementation details of database tables and fields, then it’s probably time to write your own views.

My gut reaction is that it’s going to be twice the work to try and make this work within the context of the admin than it would be to create a custom view for this. You’re trying to ignore core admin functionality here.

I understand. In fact, I have my own view and template to process these uploads, but I thought there might be a simple way to add this function to the admin panel as well.

Thank you very much for the response