コード例 #1
0
class Command(BaseCommand):

    def insert_counties(self):
        ''' Loading Counties into the Database'''
        with county_data:
            self.reader = csv.DictReader(county_data, fieldnames=['num','name'])
            for row in self.reader:
                self.county = County(name=row['name'],
                                num=row['num'])
                self.county.save()



    def insert_wards(self):
        '''Loading Wards into the Database'''
        with ward_data:
            self.reader = csv.DictReader(ward_data, fieldnames=['num','name', 'countynum'])
            for row in self.reader:
                self.ward = Ward(name=row['name'],
                            num=row['num'])

                #Try catch block connects the countynum to the ward table
                try:
                    self.ward.county = County.objects.get(num=row['countynum'])
                except:
                    pass
                else:
                    self.ward.save()



    def insert_locations(self):
        '''Loading Locations into the Database'''
        with location_data:
            self.reader = csv.DictReader(location_data, fieldnames=['num','name','wardnum', 'countynum','latitude','longitude'])
            for row in self.reader:
                self.location = Location(name=row['name'],
                                        num=row['num'],
                                        latitude=row['latitude'],
                                        longitude=row['longitude'])
                try:
                    self.location.ward = Ward.objects.get(num=row['wardnum'])
                except:
                    pass
                else:
                    self.location.save()





    def handle(self, *args, **options):
        self.insert_counties()
        self.insert_wards()
        self.insert_locations()
コード例 #2
0
 def insert_counties(self):
     ''' Loading Counties into the Database'''
     with county_data:
         self.reader = csv.DictReader(county_data, fieldnames=['num','name'])
         for row in self.reader:
             self.county = County(name=row['name'],
                             num=row['num'])
             self.county.save()