bklukaczewski
5/9/2016 - 12:29 PM

CSV dict reader; django bulk create

CSV dict reader; django bulk create

csv_reader = csv.DictReader(file, delimiter=';', quotechar='"')
imports_success = 0
object_list = []
for row in csv_reader:
  object_list.append(Machine(
    number=row['number'],
    gus_number=row['gus_number'],
    name=row['name'],
    position=get_object_or_404(Position, pk=row['position'])
  ))
  imports_success +=1
try:
  Machine.objects.bulk_create(object_list)
  messages.success(request, u'Zaimportowano %d maszyny.' % imports_success)
except:
  messages.warning(request, u'Nie udało się zaimportować maszyn.')