From 44fc9096a46c4cde99da291c7f1c3156a7595d0b Mon Sep 17 00:00:00 2001 From: Harry Marr Date: Fri, 18 Dec 2009 16:57:53 +0000 Subject: [PATCH] Added delete method to Document objects --- mongoengine/document.py | 6 ++++++ tests/document.py | 9 +++++++++ 2 files changed, 15 insertions(+) diff --git a/mongoengine/document.py b/mongoengine/document.py index a0bbda7c..5bb313ea 100644 --- a/mongoengine/document.py +++ b/mongoengine/document.py @@ -21,6 +21,12 @@ class Document(BaseDocument): object_id = self.objects._collection.save(self.to_mongo()) self.id = object_id + def delete(self): + """Delete the document from the database. This will only take effect + if the document has been previously saved. + """ + self.objects._collection.remove(self.id) + @classmethod def drop_collection(cls): """Drops the entire collection associated with this Document type from diff --git a/tests/document.py b/tests/document.py index 0d692395..fdb195de 100644 --- a/tests/document.py +++ b/tests/document.py @@ -176,6 +176,15 @@ class DocumentTest(unittest.TestCase): self.assertEqual(person_obj['age'], 30) self.assertEqual(person_obj['_id'], person.id) + def test_delete(self): + """Ensure that document may be deleted using the delete method. + """ + person = self.Person(name="Test User", age=30) + person.save() + self.assertEqual(self.Person.objects.count(), 1) + person.delete() + self.assertEqual(self.Person.objects.count(), 0) + def test_save_custom_id(self): """Ensure that a document may be saved with a custom _id. """