Model Instances Delete करना
# One object
obj = ModelName.objects.get(id=1)
obj.delete()
# Many rows
ModelName.objects.filter(field=value).delete()
एक Single Object Delete करना
पहले object fetch करें, फिर उस instance पर .delete() call करें database से सिर्फ उस एक row को हटाने के लिए।
उदाहरण: Deleting a Single Object
Fetch the object first, then call .delete() on that instance to remove just that one row from the database.
book = Book.objects.get(id=1)
book.delete()
⚠️ Run this in your own terminal or Node.js environment.
Multiple Objects Delete करना
एक filtered QuerySet पर directly .delete() call करना हर matching row को हटा देता है, सब एक database operation में।
उदाहरण: Deleting Multiple Objects
Calling .delete() directly on a filtered QuerySet removes every row that matches the filter, all in one database operation.
Book.objects.filter(genre='Horror').delete()
⚠️ Run this in your own terminal or Node.js environment.
Deletion Confirm करना
.delete() total deleted objects की संख्या वाला एक tuple return करता है, जो confirm करने के लिए useful है कि असल में क्या हुआ।
उदाहरण: Confirming Deletion
.delete() returns a tuple with the total number of objects deleted, which is useful for confirming what actually happened.
result = Book.objects.filter(pages__lt=50).delete()
print(result)
⚠️ Run this in your own terminal or Node.js environment.
- गलती से एक पूरी unfiltered QuerySet पर .delete() call करना, table में हर row wipe out करते हुए।
- यह भूल जाना कि एक object delete करना on_delete settings के आधार पर related objects को भी cascade करके delete कर सकता है।
- .delete() call करने से पहले यह confirm न करना कि object exist करता है, एक DoesNotExist error का risk लेते हुए।
- एक single fetched instance पर .delete() call करना सिर्फ उस row को हटाता है।
- एक filtered QuerySet पर .delete() call करना एक साथ हर matching row को हटाता है।
- Deletion permanent है — Django में built-in undo या trash bin नहीं है।
- Related objects ForeignKey पर set किए on_delete rule के आधार पर अपने आप delete हो सकते हैं।
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: