Reputation: 369
I want to execute an SQL query, when an item gets deleted in the admin interface. In models.py I do this:
from django.db import connection
class LList(models.Model):
item = models.ForeignKey(Item, models.DO_NOTHING)
def delete(self, *args, **kwargs):
cursor = connection.cursor()
cursor.execute('DELETE FROM some_table where item = %d', [self.item.id])
When I turn debugging on I see all the queries executed. It seems the DELETE FROM query is not executed, I only see
(0.000) DELETE FROM `llist` WHERE `llist`.`id` IN (27); args=(27,)
Why is the other DELETE FROM query not executed? How can I fix that?
Upvotes: 0
Views: 758
Reputation: 111
You are overriding the delete function the wrong way,
def delete(self, using=None, keep_parents=False):
with connection.cursor() as cursor:
cursor.execute('''DELETE FROM some_table where item = %d''', [self.item.id])
super(LList, self).delete(using, keep_parents)
But I believe the better way would be to do it using signals, you should create a pre_delete signal.
Upvotes: 1