In this post, we'll learn how to filter and delete objects in Django.

1. filtring and deleting an object using delete()

Syntax


YourModel.objects.filter().delete()
    

Example

model.py


#models.py
class test(models.Model):
    firstname = models.CharField(max_length=300)
    last_name = models.CharField(max_length=300)
    

First, let's create an object and saving it.


test.objects.create(firstname='mark', last_name='virus')

Now, we'll get all objects that contain "mark" as firstname and delete them.


test.objects.filter(firstname='mark').delete()

2. Deleting all objects

Synatx


YourModel.objects.all().delete()

Example

Deleting all Objects in test model.


test.objects.all().delete()

English today is not an art to be mastered it's just a tool to use to get a result