How to delete a record in Django models?

PythonDjangoDjango Models

Python Problem Overview


I want to delete a particular record. Such as

delete from table_name where id = 1;

How can I do this in a django model?

Python Solutions


Solution 1 - Python

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()

Solution 2 - Python

MyModel.objects.get(pk=1).delete()

this will raise exception if the object with specified primary key doesn't exist because at first it tries to retrieve the specified object.

MyModel.objects.filter(pk=1).delete()

this wont raise exception if the object with specified primary key doesn't exist and it directly produces the query

DELETE FROM my_models where id=1

Solution 3 - Python

if you want to delete one instance then write the code

entry= Account.objects.get(id= 5)
entry.delete()

if you want to delete all instance then write the code

entries= Account.objects.all()
entries.delete()

Solution 4 - Python

If you want to delete one item

wishlist = Wishlist.objects.get(id = 20)
wishlist.delete()

If you want to delete all items in Wishlist for example

Wishlist.objects.all().delete()

Solution 5 - Python

Extending the top voted answer by wolph

Note that you should pass request as a parameter to your delete function in your views. An example would be like:

from django.shortcuts import redirect


def delete(request, id):
YourModelName.objects.filter(id=id).delete()

return redirect('url_name')

Solution 6 - Python

The delete() method is used to delete model instances from a database.This method immediately deletes the object. this method returns the number of object deleted.

Example:

For deleting one record:

data_to_be_deleted = Modelname.objects.get(fieldname = value)
data_to_be_deleted.delete()

As get method returns a single object from queryset only single record will be deleted.If value supplied doesn't exist this will throw an error.If there are multilpe records in table for same value then also it will throw an error so good practice is to use a single unique value while using get.

For deleting multiple record according to a condition:

For condition based deletion filter method is used on queryset and then delete is called.

data_to_be_deleted = Modelname.objects.filter(fieldname = value)
data_to_be_deleted.delete()

For deleting all records:

For deletion of all model instances/records from database table you need to call delete method on all

data_to_be_deleted = Modelname.objects.all()
data_to_be_deleted.delete()

Note: code can be written in single line as Modelname.objects.all().delete(), but for clear understanding, I have used multiple lines.

Solution 7 - Python

It is as simple as calling the following.

SomeModel.objects.get(pk=1).delete()
# Or
SomeModel.objects.filter(pk=1).delete()

# SQL equivalent
# delete from table_name where id = 1;

In case you want to remove multiple records based on id, use the __in query lookup

SomeModel.objects.fitler(pk__in=[1,2,3,4,5,...]).delete()

# SQL equivalent
# delete from table_name where id in (1,2,4,5,...);

In case you want to delete all records, use .all() to retrieve all queries, then .delete().

SomeModel.objects.all().delete()

# SQL equivalent
# delete from table_name;

Solution 8 - Python

The way I do it:

instance = SomeModel.objects.get(id=1)

instance.delete()

For me it looks easy to understand, that's why I use this approach.

Solution 9 - Python

you can delete the objects directly from the admin panel or else there is also an option to delete specific or selected id from an interactive shell by typing in python3 manage.py shell (python3 in Linux). If you want the user to delete the objects through the browser (with provided visual interface) e.g. of an employee whose ID is 6 from the database, we can achieve this with the following code, emp = employee.objects.get(id=6).delete()

THIS WILL DELETE THE EMPLOYEE WITH THE ID is 6.

If you wish to delete the all of the employees exist in the DB instead of get(), specify all() as follows: employee.objects.all().delete()

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser426795View Question on Stackoverflow
Solution 1 - PythonWolphView Answer on Stackoverflow
Solution 2 - PythonMilad KhodabandehlooView Answer on Stackoverflow
Solution 3 - Pythonsiful islamView Answer on Stackoverflow
Solution 4 - PythonAhmed AdewaleView Answer on Stackoverflow
Solution 5 - PythonKhairulBasharView Answer on Stackoverflow
Solution 6 - PythonAshish NautiyalView Answer on Stackoverflow
Solution 7 - PythonRadwan Abu-OdehView Answer on Stackoverflow
Solution 8 - PythonlionelView Answer on Stackoverflow
Solution 9 - PythonB S Mahesh KumarView Answer on Stackoverflow