Example #1
0
    def handle(self, *args, **options):
        with open('moscow_bus_stations.csv', 'r') as csvfile:

            csv_reader = csv.DictReader(csvfile, delimiter=';')

            for line in csv_reader:
                station = Station()
                station.latitude = line["Latitude_WGS84"]
                station.longitude = line["Longitude_WGS84"]
                station.name = line["Name"]
                string = line["RouteNumbers"].split(";")
                station.save()
                for i in range(len(string)):
                    route = Route()
                    route.name = string[i].replace(" ", "")
                    route.save()
                    station.routes.add(route)
                station.save()
                del string
    def handle(self, *args, **options):
        with open('moscow_bus_stations.csv', newline='', encoding='cp1251') as csvfile:

            stations_reader = csv.DictReader(csvfile, delimiter=';')

            for row in tqdm(stations_reader):
                station = Station()
                station.name = row['Name']
                station.longitude = row['Longitude_WGS84']
                station.latitude = row['Latitude_WGS84']
                station.save()

                routes = [route for route in row['RouteNumbers'].split('; ')]
                for route in routes:
                    object = Route.objects.filter(name=route).first()
                    if object:
                        station.routes.add(object)
                    else:
                        route = Route(name=route)
                        route.save()
                        station.routes.add(route)
                station.save()