Processing uploaded csv on the basis of user input

47 views
Skip to first unread message

David Mutton

unread,
Jan 15, 2015, 9:54:13 PM1/15/15
to django...@googlegroups.com

Please let me know if this is an inappropriate question. I feel it is a little broad.

I am fairly new to Django and coding an app for educational purposes. What I am trying to achieve is to allow users to upload a CSV and then populate a model by specifying that datatype that is in each column of the CSV. I’m fine with the first half (users can upload csv which are then processed into a PropertyQuery model but currently they would need to download a template CSV file and ensure they conform to it. I’d like them to be able to upload any CSV and then specify the column’s data.

I don’t need a step by step explanation on how to achieve this but after spending a fair chunk of time searching I could use a pointer.

I hope that my flowchart below explains it better.

Many thanks.


Jani Tiainen

unread,
Jan 16, 2015, 1:55:11 AM1/16/15
to django...@googlegroups.com
Hi,

I do something similiar this way:


You could leverage Django (model)form to validate date and to normalize
it to proper data.

That way you would put design a contract between users that you state
which column names are matched to your internal data.

Then feed each row to (model)form if row is valid, you can save it, if
not you can log errors for showing them after import is done.

Another option is (what you're proposing) is to have such a dynamic
middle form to make mapping between columns frm csv and your model.
After that just map values from CSV to a dict, pass it to (model)form,
validate and save if valid.

As you see, Django (model)form is _very_ useful to validate user input
- even input is not directly from HTML form.
> <https://lh5.googleusercontent.com/-tVJ3uUxlrEk/VLh9RViDypI/AAAAAAAAA5U/fv34Zq-Y4Qs/s1600/flowchart.png>
>
>
> --
> You received this message because you are subscribed to the Google
> Groups "Django users" group. To unsubscribe from this group and stop
> receiving emails from it, send an email to
> django-users...@googlegroups.com. To post to this group,
> send email to django...@googlegroups.com. Visit this group at
> http://groups.google.com/group/django-users. To view this discussion
> on the web visit
> https://groups.google.com/d/msgid/django-users/c5acdad9-eb00-438a-bf37-faf7a66a82a5%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.


--

Jani Tiainen

David Mutton

unread,
Jan 17, 2015, 8:26:39 PM1/17/15
to django...@googlegroups.com

This was far too broad to answer but I'm posting the solution I'm currently implementing in case anyone else comes across this.

I have two models in the back end.

The first model has a JSONmodelfield (https://github.com/bradjasper/django-jsonfield) and  four intfields to save the columns numbers for street address, suburb, state, postcode)

I will use jquery and papa parse(http://papaparse.com/) on a modelform to process the csv file clientside, save the json into the hidden JSONmodelfield and create dropdown selections from the csv headers the selection from each of these dropdowns is also passed to the hidden  intfields. On save() the json is processed using the values in the intfields to populate a second model.

The second model just has 4 charfields for the street address, suburb, state, postcode.

Reply all
Reply to author
Forward
0 new messages