conn = MySQLdb.connect( host=db_hostname,user=db_username,passwd=db_password,db=db_name,use_unicode=True ) cursor = conn.cursor( MySQLdb.cursors.DictCursor ) query = "SELECT * FROM calls ORDER BY CallID DESC" cursor.execute( query ) rows = cursor.fetchall() count = 0 #can do jurisdiction outside of the loop cause it won't change. juri = Jurisdiction.objects.get( pk=1 ) for row in rows: try: beat = Beat.objects.get(name=str( row[ "Beat" ] ) ) except: print "No Beat found for %s" % row[ "Beat" ] beat = Beat() beat.name = row[ "Beat" ] beat.jurisdiction = juri beat.save() try: call_type = CallType.objects.get( name__iexact=row[ "CallType" ].strip().upper() ) #print "FOUND call_type: %s" % call_type except: print bail() try: call = Call.objects.get( event_num__iexact = row[ "EventNum" ].strip() ) action = "UPDATE" except: call = Call() action = "INSERT"
row_count = 0 for call in calls: if len(call.keys()) == 0: pass else: #print call try: if call[ "beat" ] == None: call[ "beat" ] = "" print "Setting beat to an empty string instead of None" beat = Beat.objects.get( name__iexact = str( call[ "beat" ] ) ) print "FOUND beat: %s" % call[ "beat" ] except: bail() print "No Beat found for %s" % call[ "beat" ] beat = Beat() beat.name = call[ "beat" ] beat.jurisdiction = juri beat.save() try: calltype = CallType.objects.get( name__iexact = str( call[ "calltype" ] ) ) print "FOUND calltype: %s" % call[ "calltype" ] except: bail() print "didn't find a calltype for %s" % call[ "calltype" ] calltype = CallType( name = call[ "calltype" ], jurisdiction = juri, active = True ) calltype.save() try: new_call = Call.objects.get( event_num__iexact = str( call[ "event_num" ] ) )