[Django]-Raw SQL query in Django

6👍

You can delete your record(s) directly via connection.cursor() (docs):

from django.db import connection

cursor = connection.cursor()
cursor.execute('DELETE FROM costumer_worker WHERE costumer_ptr_id = %s', [costumer.id])
connection.commit()

But, the thing you’re trying to do looks too simple to write SQL directly, use django ORM instead.

👤alecxe

2👍

You may be missing comitting the transcation. Based on your settings, you may need to add :
transaction.commit_unless_managed() in django 1.4 ( not needed in django 1.5, since default settings are different)

0👍

use this line:

from django.db import connection, transaction
cursor = connection.cursor()
with transaction.commit_on_success():
   cursor.execute('DELETE FROM costumer_worker WHERE costumer_ptr_id = %s', [costumer.id])
   connection.commit()

Leave a comment