From ae81458f7ad5606acef0015120276c80b89a65e1 Mon Sep 17 00:00:00 2001 From: Sanj Date: Thu, 15 Nov 2012 14:05:18 +0530 Subject: [PATCH] import csv function, minor db change --- ifa/films/imports.py | 16 ++++++++++++++++ ifa/films/models.py | 3 ++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/ifa/films/imports.py b/ifa/films/imports.py index c8acad4..306488c 100644 --- a/ifa/films/imports.py +++ b/ifa/films/imports.py @@ -1,5 +1,7 @@ path = "/home/sanj/c/ifa/tmp/FILMS.txt" +csv_path = "/home/sanj/c/ifa/tmp/Films.sorted.csv" from models import * +import csv def do(path=path): errors = open("errors.txt", "w") @@ -26,3 +28,17 @@ def do(path=path): errors.write(txt) print title errors.close() + +def import_csv(path=csv_path): + lines = csv.reader(open(path)) + for line in lines: + d = { + 'title': line[0], + 'year': int(line[1].replace('"', '')[0:4]), + 'director': line[2], + 'producer': line[3], + 'language': line[4] + } + f = Film(**d) + f.save() + print d['title'] diff --git a/ifa/films/models.py b/ifa/films/models.py index 3de9aeb..7a7f6fa 100644 --- a/ifa/films/models.py +++ b/ifa/films/models.py @@ -2,7 +2,8 @@ from django.db import models class Film(models.Model): title = models.CharField(max_length=512) - director = models.CharField(max_length=512) + director = models.CharField(max_length=512, blank=True) + producer = models.CharField(max_length=512, blank=True) language = models.CharField(max_length=64, blank=True) year = models.IntegerField(max_length=4, blank=True, null=True) available = models.BooleanField(default=False)