Оценок пока нет Insert bulk data into table in Django/PostgreSQL

There are other ways how to add a large amount of data, this method is suitable if the number of rows does not exceed 10,000 and assumes that your server is able to withstand these requests

def insert_cities(cities):
    dbname, tablename = 'MyDB', 'city_coordinates'

    with connections[dbname].cursor() as cursor:
        for city in cities:
            cursor.execute(f"""
            INSERT INTO {tablename} (record_id, X, Y, record_id_2, city_id, city_name_heb, secondary_id, city_type_id, city_type_name, cuty_name_eng) 
            VALUES(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)""", 
            (city['record_id'], city['X'], city['Y'], city['record_id_2'], city['city_id'], city['city_name_heb'], city['secondary_id'], city['city_type_id'], city['city_type_name'], city['city_name_eng']))
            connections[dbname].commit()

        cursor.close()
        connections[dbname].close()

Пожалуйста, оцените материал

WebSofter

Web - технологии