T
T
to_east2018-11-12 22:22:41
PostgreSQL
to_east, 2018-11-12 22:22:41

Django orm mapping raw INSERT, UPDATE, DELETE queries to objects?

Hello everyone!
This function takes a list of urls, and creates entries in the table in batch:

@transaction.atomic
def site_bulk_create(urls):
    sql = 'INSERT INTO public.app_core_site (url) VALUES '
    values = ["('{}')".format(url) for url in urls]
    sql += ', '.join(values)
    sql += ' RETURNING *'

    with connection.cursor() as cursor:
        cursor.execute(sql)
        return cursor.fetchall()

The result is a list of tuples like this [(1, ' foo.com '), ...].
It would be nice if a list with objects of the Site entity was returned, as if
I were doing a query like this Site.objects.raw('...')

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dimonchik, 2018-11-12
@dimonchik2013

it would be bad
either ORM or RAW SQL

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question