miguelfg
miguelfg

Reputation: 1524

importing csv file in django with django-adaptors

I have installed it, imported it in my models.py, created my CSV class like this:

class CsvProvince(CsvModel):

    class Meta:
        delimiter = ";"
        has_header = True
        dbModel = Province

But I don't understand how to run the import, in documentation is said to do:

>>> my_csv_list = CsvProvince.import_data(data = open("my_csv_file_name.csv"))
>>> first_line = my_csv_list[0]

but don't know exactly how/where to run it, if I open the shell (I use PyCharm IDE) it doesn't find my classes (CsvProvince), or could I write it all in a .py and execute it?. But I don't get which imports I should do, and where should run it, inside my Django app?, wherever?...

Thanks in advance

Upvotes: 2

Views: 775

Answers (2)

antonyoni
antonyoni

Reputation: 909

If you're using the Django shell in PyCharm (Ctrl+Alt+R > Shell), you'll still need to import the class with a:

>>> from <your-app>.models import CsvProvince

Upvotes: 1

FastTurtle
FastTurtle

Reputation: 2311

You probably want to use django's shell. This will load all of your django settings so you can test or use your models interactively.

./manage.py shell

>>> from myapp import CsvProvince  # Replace myapp with your app's name
>>> my_csv_list = CsvProvince.import_data(data = open("my_csv_file_name.csv"))
>>> first_line = my_csv_list[0]

Upvotes: 2

Related Questions