Added initial implementation of cascading document deletion.

The current implementation is still very basic and needs some polish.
The essence of it is that each Document gets a new meta attribute called
"delete_rules" that is a dictionary containing (documentclass,
fieldname) as key and the actual delete rule as a value.  (Possible
values are DO_NOTHING, NULLIFY, CASCADE and DENY.  Of those, only
CASCADE is currently implented.)
This commit is contained in:
Vincent Driessen
2010-12-05 08:08:55 -08:00
parent 4f3eacd72c
commit 86233bcdf5
4 changed files with 56 additions and 2 deletions

View File

@@ -624,6 +624,31 @@ class DocumentTest(unittest.TestCase):
BlogPost.drop_collection()
def test_cascade_delete(self):
"""Ensure that a referenced document is also deleted upon deletion.
"""
class BlogPost(Document):
meta = {'collection': 'blogpost_1'}
content = StringField()
author = ReferenceField(self.Person, delete_rule=CASCADE)
self.Person.drop_collection()
BlogPost.drop_collection()
author = self.Person(name='Test User')
author.save()
post = BlogPost(content = 'Watched some TV')
post.author = author
post.save()
# Delete the Person, which should lead to deletion of the BlogPost, too
author.delete()
self.assertEqual(len(BlogPost.objects), 0)
def tearDown(self):
self.Person.drop_collection()