views:

43

answers:

1

I want to delete a record. But I of a particular record. Such as

delete from table_name where id = 1;

How can I do this in a django model?

I am writing a function:

    def delete(id):`

       -----------
+5  A: 

There are a couple of ways:

To delete it directly:

SomeModel.objects.filter(id=id).delete()

To delete it from an instance:

instance = SomeModel.objects.get(id=id)
instance.delete()
WoLpH
The wolf indeed howls true. +1.
Manoj Govindan
Note that the first one will not call the .delete() method of the object, so if you have 'cleanup' code in that method it will not be called. Generally not an issue, but worth keeping in mind.
Matthew Schinckel
@Matthew Schinckel: that is true. If you want to have a custom delete method than you should be using the `pre_delete` or `post_delete` signal instead.
WoLpH