Search code examples
pythondjangodjango-formsdjango-file-upload

Get header of CSV file in Django ModelForm


I'm building a django app that will let people upload CSVs that will be stored in S3 (based on django-storages) then processed by celery tasks which will then ingest them into the database.

These CSVs can be any size, from a few rows (where Django would store it as a InMemoryUploadedFile) to hundreds of megabytes (where django would use aTemporaryUploadedFile.)

I'm using a simple ModelForm on a generic CreateView, but I want to add a way to check the first row in the file (the header) to validate that the CSV has all the columns I need.

This would be pretty simple if I knew the path of the file. I'd just use python's standard file and csv handling functionality to read the first line into the csv module and check the fields. But how do I do that using an uploaded file where it may be in memory or it may be a temporary file, inside the modelform?


Solution

  • This answer will not spell it out for you, but it should be enough to unblock you.

    From the looks of the Django documentation, you can use a built in method to get the temporary file path of a "TemporaryUploadedFile":

    TemporaryUploadedFile.temporary_file_path()
    

    To wrap this all up, you'll have to have some logic in your code to identify if it is an "InMemoryUploadedFile" or a "TemporaryUploadedFile". From there, you can look at each object differently to assess the columns.