で知りました。bulk createを使うそう(Django 1.4かららしい)
bulk createを使った場合
def read_user_data(path):
with open(path) as f:
reader = csv.reader(f, delimiter=',')
_header = next(reader)
User.objects.bulk_create([User(
id=row[0],
hoge=row[1],
) for row in reader])
forを使った場合
def read_user_data(self):
path = self.user_data_path
with open(path) as f:
reader = csv.reader(f)
_header = next(reader)
for row in reader:
_, created = User.objects.get_or_create(
id=row[0],
hoge=row[1],
)
こんなModel
from django.db import models
class User(models.Model):
id = models.PositiveIntegerField(primary_key=True)
hoge = models.CharField(max_length=128)