Examples


Django Delete Data


Deleting data in Django refers to the process of removing existing records from the database table. This may be accomplished the usage of Django`s ORM (Object-Relational Mapping) capabilities.

To open a Python shell, use the command is given below

python manage.py shell

Now we are in the shell, the result should be something like this:

D:\django_projects\myproject>python manage.py shell
Python 3.9.0 (tags/v3.9.0:9cf6752, Oct  5 2020, 15:34:40) [MSC v.1927 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>>

Delete Records

To delete a record in a table, start by getting the record you want to delete

>>> from myapp.models import Product
>>> a = Product.objects.all()[1]

a will now stand for the "Orange" product at index 1, but let's confirm that it is accurate.

>>> a.product_name

This should give you this result

Orange

Now we can delete the record

>>> a.delete()

Deleting the Record

To delete the selected record, use the following command

a.delete()

Output

>>> a.delete()
(1, {'myapp.Product': 1})
>>>

Prev Next