DELETE Operation in django
Now, lets learn how to perform delete operation using django.
First of all, let us import vaccine and campaign model.
>>> from vaccine.models import Vaccine
>>> from campaign.models import Campaign
Now, if you execute this query, you will get a queryset containing a campaign object.
>>> Campaign.objects.all()
Now, we will print the id of all vaccine objects.
>>> for object in Vaccine.objects.all():
... print(object.id)
...
1
Get the instance of any one of vaccine object.
>>> vaccine = Vaccine.objects.get(id=1)
To delete an object in django, you need to use delete method.
>>> vaccine.delete()
(4, {'campaign.Campaign_agents': 2, 'vaccine.Vaccine': 1, 'campaign.Campaign': 1})
This line of code delete the vaccine instance and all the campaign objects with which it has any relationships because we have specified on_delete = models.CASCADE.
In this way, we have learned how to perform CREATE, READ, UPDATE and DELETE operation using django ORM.
[Commit the changes and push it in the remote repository.]