kramer65
kramer65

Reputation: 53943

How to get the number of inserts from a raw INSERT query in Django?

I'm running a fairly large insert into select query in Django, which I do using

MyModel.objects.raw(insert_query)

But that doesn't do anything since queries seem to only be run when something is taken from it's result. So when I do this it runs the query

MyModel.objects.raw(insert_query)[0]

but also gives an error:

TypeError: 'NoneType' object is not iterable

I can run the queries using

from django.db import connection
with connection.cursor() as cursor:
    cursor.execute(insert_query)

But that doesn't give me any feedback on what it actually did.

Is there a way that I can run the raw query and get the number of records it inserted or a possible error if that occurred?

Upvotes: 0

Views: 276

Answers (1)

iklinac
iklinac

Reputation: 15738

You could get number of records added through

cursor.rowcount

Upvotes: 1

Related Questions