# intended to run in shell plus import csv from django.db import connection from catalog.models import ZipCode ADMIN_USERNAME = 'jcosta' PATH_TO_FILE = "/Users/jcosta/projects/pbp_store/pbp_store/catalog/extra/hancock_only_zips" user = User.objects.get(username=ADMIN_USERNAME) cr = csv.reader(open(PATH_TO_FILE, "rb")) for row in cr: try: try: zc = ZipCode.objects.get(zipcode=row[0]) except ZipCode.DoesNotExist: zc = ZipCode.objects.create(zipcode=row[0], created_by_id=user.id, last_updated_by_id=user.id) zc.city=row[1] zc.state=row[2] zc.county=row[3] zc.save() if zc.id % 1000 == 0: print "Processed zip code: {0}".format(zc.zipcode) except Exception, e: connection._rollback() print "Another exception: {0}".format(e) print row