So assuming your models.py
looks something like this:
class Representative(models.Model):
parliament = models.CharField(max_length=128)
name = models.CharField(max_length=128)
country = models.CharField(max_length=128)
party_group = models.CharField(max_length=128)
national_party = models.CharField(max_length=128)
position = models.CharField(max_length=128)
You can then run python manage.py shell
and execute the following:
import csv
from your_app.models import Representative
# If you're using different field names, change this list accordingly.
# The order must also match the column order in the CSV file.
fields = ['parliament', 'name', 'country', 'party_group', 'national_party', 'position']
for row in csv.reader(open('your_file.csv')):
Representative.objects.create(**dict(zip(fields, row)))
And you're done.
Addendum (edit)
Per Thomas's request, here's an explanation of what **dict(zip(fields,row))
does:
So initially, fields
contains a list of field names that we defined, and row
contains a list of values that represents the current row in the CSV file.
fields = ['parliament', 'name', 'country', ...]
row = ['7', 'Marta Andreasen', 'United Kingdom', ...]
What zip()
does is it combines two lists into one list of pairs of items from both lists (like a zipper); i.e. zip(['a','b,'c'], ['A','B','C'])
will return [('a','A'), ('b','B'), ('c','C')]
. So in our case:
>>> zip(fields, row)
[('parliament', '7'), ('name', 'Marta Andreasen'), ('country', 'United Kingdom'), ...]
The dict()
function simply converts the list of pairs into a dictionary.
>>> dict(zip(fields, row))
{'parliament': '7', 'name': 'Marta Andreasen', 'country': 'United Kingdom', ...}
The **
is a way of converting a dictionary into a keyword argument list for a function. So function(**{'key': 'value'})
is the equivalent of function(key='value')
. So in out example, calling create(**dict(zip(field, row)))
is the equivalent of:
create(parliament='7', name='Marta Andreasen', country='United Kingdom', ...)
Hope this clears things up.