1

I am trying to import data from a CSV file into an SQL database through my Django Project. I have made my models and a separate function in Pandas that extracts the specific information that I need from the CSV file itself. I am trying to use the Django Shell to load the data from the CSV file into my database models that I created in my models.py file.

However the shell can't find the CSV file when I run my function (in pandas). How do I call in or import the CSV file from my directory into the shell? Then I'll be able to use my function to extract specific data and store it in the models I created for my SQL database.

Please help. I cannot share my scripts online for good reasons however I'd just like to know how to import a CSV file into the DJango Shell from my directory, in order to manipulate it with code. I'd also love to hear how you would import specific sets of data and store it in your classes. Does my logic of using a pandas based CSV reader function to store data into my database models sound reasonable? Is there a better way to do it?

Thank you!

-Felix

Felix Dasgupta
  • 127
  • 1
  • 2
  • 10
  • 1
    Reference the csv file with an absolute path. Instead of `with file('my.csv') as f:` do something like `with file('/usr/local/my.csv') as f:` It should be fine for a one-off import. Might need to change your method if this is the only way you plan on adding data to your database. – Stack of Pancakes Oct 11 '14 at 02:16
  • @StackofPancakes Agreed. I would also look into writing a management command for importing the data from you CSV. https://docs.djangoproject.com/en/dev/howto/custom-management-commands/ – Paul Oct 11 '14 at 02:44
  • I will also look into that, thank you @Paul – Felix Dasgupta Oct 13 '14 at 17:55

0 Answers0