Behind the Scenes: From HTML Form to StorageWed 21 September 2011 under django
Cross posted from my work blog post
In this post, we are going to see what happens behind the scenes when a file is uploaded to a django powered web application.
An HTML form with a file input (atleast one) and encoding set to multipart/form-data is submitted. The MultiPartParser parses the POST request and returns a tuple of the POST and FILES data (request.POST, request.FILES). The MultiPartParser processes the uploaded data using the File Upload Handlers objects (through the new_file, receive_data_chunk and upload_complete methods). The request.FILES values are a sequence of instances of UploadedFile.
In the django form, we pass the request.FILES MultiValueDict. These UploadedFile instances are validated by the full_clean method on the Form. The full_clean method in turn calls the _clean_fields method which calls the clean method on the forms.FileField and checks if the data is empty.
After the form is successfully validated, we might assign and save the cleaned_data of the form to a model instance (or by saving the ModelForm). When the save method on the model instance is called, the save_base method calls a pre_save method on each field of the model. This pre_save method returns the value of the file instance bound to that FileField and calls it's save method. This save method (on the models.FileField) in turn calls the save (Storage) method on the Storage which is either picked up from the arguments passed to the FileField (FileField(storage=...)) or the DEFAULT_FILE_STORAGE settings attribute.
This is the flow from the HTML Form all the way upto the File Storage. Hope you liked it.