Rename delete_rule -> reverse_delete_rule.
This commit is contained in:
parent
52f5deb456
commit
07ef58c1a7
@ -200,8 +200,8 @@ documents that other documents still hold references to will lead to consistency
|
|||||||
issues. Mongoengine's :class:`ReferenceField` adds some functionality to
|
issues. Mongoengine's :class:`ReferenceField` adds some functionality to
|
||||||
safeguard against these kinds of database integrity problems, providing each
|
safeguard against these kinds of database integrity problems, providing each
|
||||||
reference with a delete rule specification. A delete rule is specified by
|
reference with a delete rule specification. A delete rule is specified by
|
||||||
supplying the :attr:`delete_rule` attribute on the :class:`ReferenceField`
|
supplying the :attr:`reverse_delete_rule` attributes on the
|
||||||
definition, like this::
|
:class:`ReferenceField` definition, like this::
|
||||||
|
|
||||||
class Employee(Document):
|
class Employee(Document):
|
||||||
...
|
...
|
||||||
|
@ -191,7 +191,7 @@ class DocumentMetaclass(type):
|
|||||||
new_class = super_new(cls, name, bases, attrs)
|
new_class = super_new(cls, name, bases, attrs)
|
||||||
for field in new_class._fields.values():
|
for field in new_class._fields.values():
|
||||||
field.owner_document = new_class
|
field.owner_document = new_class
|
||||||
delete_rule = getattr(field, 'delete_rule', DO_NOTHING)
|
delete_rule = getattr(field, 'reverse_delete_rule', DO_NOTHING)
|
||||||
if delete_rule != DO_NOTHING:
|
if delete_rule != DO_NOTHING:
|
||||||
field.document_type.register_delete_rule(new_class, field.name,
|
field.document_type.register_delete_rule(new_class, field.name,
|
||||||
delete_rule)
|
delete_rule)
|
||||||
|
@ -418,13 +418,13 @@ class ReferenceField(BaseField):
|
|||||||
access (lazily).
|
access (lazily).
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, document_type, delete_rule=DO_NOTHING, **kwargs):
|
def __init__(self, document_type, reverse_delete_rule=DO_NOTHING, **kwargs):
|
||||||
if not isinstance(document_type, basestring):
|
if not isinstance(document_type, basestring):
|
||||||
if not issubclass(document_type, (Document, basestring)):
|
if not issubclass(document_type, (Document, basestring)):
|
||||||
raise ValidationError('Argument to ReferenceField constructor '
|
raise ValidationError('Argument to ReferenceField constructor '
|
||||||
'must be a document class or a string')
|
'must be a document class or a string')
|
||||||
self.document_type_obj = document_type
|
self.document_type_obj = document_type
|
||||||
self.delete_rule = delete_rule
|
self.reverse_delete_rule = reverse_delete_rule
|
||||||
super(ReferenceField, self).__init__(**kwargs)
|
super(ReferenceField, self).__init__(**kwargs)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
|
@ -625,14 +625,14 @@ class DocumentTest(unittest.TestCase):
|
|||||||
BlogPost.drop_collection()
|
BlogPost.drop_collection()
|
||||||
|
|
||||||
|
|
||||||
def test_delete_rule_cascade_and_nullify(self):
|
def test_reverse_delete_rule_cascade_and_nullify(self):
|
||||||
"""Ensure that a referenced document is also deleted upon deletion.
|
"""Ensure that a referenced document is also deleted upon deletion.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
class BlogPost(Document):
|
class BlogPost(Document):
|
||||||
content = StringField()
|
content = StringField()
|
||||||
author = ReferenceField(self.Person, delete_rule=CASCADE)
|
author = ReferenceField(self.Person, reverse_delete_rule=CASCADE)
|
||||||
reviewer = ReferenceField(self.Person, delete_rule=NULLIFY)
|
reviewer = ReferenceField(self.Person, reverse_delete_rule=NULLIFY)
|
||||||
|
|
||||||
self.Person.drop_collection()
|
self.Person.drop_collection()
|
||||||
BlogPost.drop_collection()
|
BlogPost.drop_collection()
|
||||||
@ -656,18 +656,18 @@ class DocumentTest(unittest.TestCase):
|
|||||||
author.delete()
|
author.delete()
|
||||||
self.assertEqual(len(BlogPost.objects), 0)
|
self.assertEqual(len(BlogPost.objects), 0)
|
||||||
|
|
||||||
def test_delete_rule_cascade_recurs(self):
|
def test_reverse_delete_rule_cascade_recurs(self):
|
||||||
"""Ensure that a chain of documents is also deleted upon cascaded
|
"""Ensure that a chain of documents is also deleted upon cascaded
|
||||||
deletion.
|
deletion.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
class BlogPost(Document):
|
class BlogPost(Document):
|
||||||
content = StringField()
|
content = StringField()
|
||||||
author = ReferenceField(self.Person, delete_rule=CASCADE)
|
author = ReferenceField(self.Person, reverse_delete_rule=CASCADE)
|
||||||
|
|
||||||
class Comment(Document):
|
class Comment(Document):
|
||||||
text = StringField()
|
text = StringField()
|
||||||
post = ReferenceField(BlogPost, delete_rule=CASCADE)
|
post = ReferenceField(BlogPost, reverse_delete_rule=CASCADE)
|
||||||
|
|
||||||
|
|
||||||
author = self.Person(name='Test User')
|
author = self.Person(name='Test User')
|
||||||
@ -690,14 +690,14 @@ class DocumentTest(unittest.TestCase):
|
|||||||
BlogPost.drop_collection()
|
BlogPost.drop_collection()
|
||||||
Comment.drop_collection()
|
Comment.drop_collection()
|
||||||
|
|
||||||
def test_delete_rule_deny(self):
|
def test_reverse_delete_rule_deny(self):
|
||||||
"""Ensure that a document cannot be referenced if there are still
|
"""Ensure that a document cannot be referenced if there are still
|
||||||
documents referring to it.
|
documents referring to it.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
class BlogPost(Document):
|
class BlogPost(Document):
|
||||||
content = StringField()
|
content = StringField()
|
||||||
author = ReferenceField(self.Person, delete_rule=DENY)
|
author = ReferenceField(self.Person, reverse_delete_rule=DENY)
|
||||||
|
|
||||||
self.Person.drop_collection()
|
self.Person.drop_collection()
|
||||||
BlogPost.drop_collection()
|
BlogPost.drop_collection()
|
||||||
|
@ -734,12 +734,12 @@ class QuerySetTest(unittest.TestCase):
|
|||||||
self.Person.objects.delete()
|
self.Person.objects.delete()
|
||||||
self.assertEqual(len(self.Person.objects), 0)
|
self.assertEqual(len(self.Person.objects), 0)
|
||||||
|
|
||||||
def test_delete_rule_cascade(self):
|
def test_reverse_delete_rule_cascade(self):
|
||||||
"""Ensure cascading deletion of referring documents from the database.
|
"""Ensure cascading deletion of referring documents from the database.
|
||||||
"""
|
"""
|
||||||
class BlogPost(Document):
|
class BlogPost(Document):
|
||||||
content = StringField()
|
content = StringField()
|
||||||
author = ReferenceField(self.Person, delete_rule=CASCADE)
|
author = ReferenceField(self.Person, reverse_delete_rule=CASCADE)
|
||||||
BlogPost.drop_collection()
|
BlogPost.drop_collection()
|
||||||
|
|
||||||
me = self.Person(name='Test User')
|
me = self.Person(name='Test User')
|
||||||
@ -755,7 +755,7 @@ class QuerySetTest(unittest.TestCase):
|
|||||||
self.Person.objects(name='Test User').delete()
|
self.Person.objects(name='Test User').delete()
|
||||||
self.assertEqual(1, BlogPost.objects.count())
|
self.assertEqual(1, BlogPost.objects.count())
|
||||||
|
|
||||||
def test_delete_rule_nullify(self):
|
def test_reverse_delete_rule_nullify(self):
|
||||||
"""Ensure nullification of references to deleted documents.
|
"""Ensure nullification of references to deleted documents.
|
||||||
"""
|
"""
|
||||||
class Category(Document):
|
class Category(Document):
|
||||||
@ -763,7 +763,7 @@ class QuerySetTest(unittest.TestCase):
|
|||||||
|
|
||||||
class BlogPost(Document):
|
class BlogPost(Document):
|
||||||
content = StringField()
|
content = StringField()
|
||||||
category = ReferenceField(Category, delete_rule=NULLIFY)
|
category = ReferenceField(Category, reverse_delete_rule=NULLIFY)
|
||||||
|
|
||||||
BlogPost.drop_collection()
|
BlogPost.drop_collection()
|
||||||
Category.drop_collection()
|
Category.drop_collection()
|
||||||
@ -780,13 +780,13 @@ class QuerySetTest(unittest.TestCase):
|
|||||||
self.assertEqual(1, BlogPost.objects.count())
|
self.assertEqual(1, BlogPost.objects.count())
|
||||||
self.assertEqual(None, BlogPost.objects.first().category)
|
self.assertEqual(None, BlogPost.objects.first().category)
|
||||||
|
|
||||||
def test_delete_rule_deny(self):
|
def test_reverse_delete_rule_deny(self):
|
||||||
"""Ensure deletion gets denied on documents that still have references
|
"""Ensure deletion gets denied on documents that still have references
|
||||||
to them.
|
to them.
|
||||||
"""
|
"""
|
||||||
class BlogPost(Document):
|
class BlogPost(Document):
|
||||||
content = StringField()
|
content = StringField()
|
||||||
author = ReferenceField(self.Person, delete_rule=DENY)
|
author = ReferenceField(self.Person, reverse_delete_rule=DENY)
|
||||||
|
|
||||||
BlogPost.drop_collection()
|
BlogPost.drop_collection()
|
||||||
self.Person.drop_collection()
|
self.Person.drop_collection()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user