('03', "Meerut"), ('44', "Mirzapur"), ('19', "Moradabad"), ('02', "Muzaffarnagar"), ('18', "Pilibhit"), ('31', "Pratapgarh"), ('59', "Rae Bareli"), ('20', "Rampur"), ('01', "Saharanpur"), ('75', "Sambhal"), ('56', "Sant Kabir Nagar"), ('46', "Sant Ravidas Nagar"), ('17', "Shahjahanpur"), ('74', "Shamli"), ('66', "Shrawasti"), ('55', "Siddharthnagar"), ('60', "Sitapur"), ('45', "Sonbhadra"), ('68', "Sultanpur"), ('58', "Unnao"), ('40', "Varanasi"), ] UP = IndianState.objects.get(shortcode='UP') for number, name in DISTRICTS: name = string.capwords(name) district = IndianDistrict(state=UP, name=name, number=number) district.save() print district
except ObjectDoesNotExist: pass for i in reversed(xrange(5, len(s) + 1)): try: return IndianState.objects.get(name__istartswith=s[:i]) except ObjectDoesNotExist: pass raise ValueError("Could not find state %s" % s) def get_districts(state): districts = state.next_sibling.next_sibling for line in repr(districts).split('\n'): line = clean_district(line) if line: yield line states = soup.findAll('h2')[1:] for state in states: state_name = clean_state(repr(state.u)) print state_name stateobj = get_state_object(state_name) print stateobj if stateobj.shortcode in [ 'KA', 'UP' ]: continue districts = get_districts(state) for d in sorted(districts): districtobj = IndianDistrict(state=stateobj, name=d) districtobj.save() print ' ', districtobj print "====================================="