def parse_row(r): from unesco.models import Category, Region, Iso, States, Site # Category try: ca = Category.objects.get(name=r['category']) except: ca = Category(name=r['category']) ca.save() # Region try: re = Region.objects.get(name=r['region']) except: re = Region(name=r['region']) re.save() # Iso try: iso = Iso.objects.get(name=r['iso']) except: iso = Iso( name=r['iso'], region=re) iso.save() # States try: st = States.objects.get(name=r['states']) except: st = States( name=r['states'], region = re, iso = iso ) st.save() # Site try: # check year year = int(r['year']) except: year = None try: longitude = float(r['longitude']) except: longitude = None try: latitude = float(r['latitude']) except: latitude = None try: area_hectares = float(r['area_hectares']) except: area_hectares = None si = Site( name = r['name'], description = r['description'], justification = r['justification'], year = year, longitude = longitude, latitude = latitude, area_hectares = area_hectares, category = ca, region = re, iso = iso, state = st) si.save()
# if i > 5 : break Site.objects.all().delete() Category.objects.all().delete() Region.objects.all().delete() States.objects.all().delete() Iso.objects.all().delete() for row in rows[1:]: print(row) try: c = Category.objects.get(name=row[7]) except: print("Inserting category", row[7]) c = Category(name=row[7]) c.save() try: s = States.objects.get(name=row[8]) except: print("Inserting states", row[8]) s = States(name=row[8]) s.save() try: r = Region.objects.get(name=row[9]) except: print("Inserting region", row[9]) r = Region(name=row[9]) r.save()
Site.objects.all().delete() # name,description,justification,year,longitude,latitude,area_hectares,category,states,region,iso for row in reader: # print(row) ############## Category # name for Category try: c_name = string(row[7]) except: c_name = None c = Category(name=c_name) c.save() ################ Region # name for Region try: r_name = string(row[9]) except: r_name = None # latitude for Region try: latitude = float(row[5]) except: latitude = None
States.objects.all().delete() Region.objects.all().delete() Iso.objects.all().delete() i = 0 for row in rows: if len(row[0]) < 1: continue print(row[0]) i = i + 1 if i > 5: break try: a = Category.objects.get(name=row[0]) except: print("Inserting name", row[0]) a = Category(name=row[0]) a.save() try: b = States.objects.get(description=row[1]) except: print("Inserting description", row[1]) b = States(description=row[1]) b.save() try: c = States.objects.get(justification=row[2]) except: print("Inserting justification", row[2]) c = States(justification=row[2]) c.save()
try: lat = float(row[5]) except: lat = None try: ah = float(row[6]) except: ah = None try: cat = Category.objects.get(name=row[7]) except: print("New category", row[7]) cat = Category(name=row[7]) cat.save() try: st = States.objects.get(name=row[8]) except: print("New states", row[8]) st = States(name=row[8]) st.save() try: reg = Region.objects.get(name=row[9]) except: print("New region", row[9]) reg = Region(name=row[9]) reg.save()