djangodata-objects

Django: Correct way of passing data object from view to template to view?


From a template users can upload a csv file which gets parsed in

def parseCSV(request):
  magic happens here (conforming date formats and all such fun things)
  return column names to template

This view returns a list of columns and the user is asked to pick x columns to save. The users choice is posted to

def saveCSV(request):
  logic for saving

Now my question is, how do I most correctly handle the csv data object between view 1 and 2? Do i save it as a temperary file or do i send it back and forth view1->template->view2 as a data object? Or maybe something third?


Solution

  • There is no "correct" way as it all depends on the concrete situation. In this case, it depends on the size of the data from the CSV file. Given that the data is rather large, the best approach is most likely to store the parsed data on the server, and then in the next request only send the user's selection of the full data set.

    I would suggest you to parse the data and store it as a JSON blob in the database, so that you can easily retrieve it for the next request. This way you can send the user's selection of rows and columns (or "coordinates"), and save that as real data afterwards. The benefit of storing it right away is that the user can return to the process even after leaving the flow. The downside is, though, that you save unused data, if the user never completes the process, and you might need to clear this later. If you store it in a table containing only temporary data, it should ease the cleaning process.