EmbeddedDocuments dont support Reverse Delete Rules

Now throws an InvalidDocumentError

Refs #227
This commit is contained in:
Ross Lawley 2011-12-02 02:46:55 -08:00
parent d06c5f036b
commit e231f71b4a
3 changed files with 25 additions and 2 deletions

View File

@ -8,10 +8,11 @@ Upgrading
Embedded Documents - if you had a `pk` field you will have to rename it from `_id`
to `pk` as pk is no longer a property of Embedded Documents.
Reverse Delete Rules in Embedded Documents, MapFields and DictFields now throw
an InvalidDocument error as they aren't currently supported.
Document._get_subclasses - Is no longer used and the class method has been removed.
Reverse Delete Rules on MapFields and DictFields now throw a InvalidDocument error
as they aren't supported.
0.4 to 0.5
===========

View File

@ -518,6 +518,8 @@ class DocumentMetaclass(type):
f = field.field
if delete_rule != DO_NOTHING:
if issubclass(new_class, EmbeddedDocument):
raise InvalidDocumentError("Reverse delete rules are not supported for EmbeddedDocuments (field: %s)" % field.name)
f.document_type.register_delete_rule(new_class, field.name, delete_rule)
if field.name and hasattr(Document, field.name) and EmbeddedDocument not in new_class.mro():
@ -663,6 +665,18 @@ class TopLevelDocumentMetaclass(DocumentMetaclass):
return new_class
def __instancecheck__(cls, inst):
"""Custom instance check for isinstance() as registering delete rules or
calling a cls method in __new__ seems to change the cls so vanilla
isinstance() fails"""
is_instance = super(DocumentMetaclass, cls).__instancecheck__(inst)
if hasattr(cls, '_meta') and 'delete_rules' in cls._meta and not is_instance:
try:
is_instance = get_document(cls.__name__) == get_document(inst.__class__.__name__)
except NotRegistered:
pass
return is_instance
@classmethod
def _unique_with_indexes(cls, new_class, namespace=""):
unique_indexes = []

View File

@ -2245,6 +2245,8 @@ class DocumentTest(unittest.TestCase):
author.delete()
self.assertEqual(len(BlogPost.objects), 0)
def test_invalid_reverse_delete_rules_raise_errors(self):
def throw_invalid_document_error():
class Blog(Document):
content = StringField()
@ -2253,6 +2255,12 @@ class DocumentTest(unittest.TestCase):
self.assertRaises(InvalidDocumentError, throw_invalid_document_error)
def throw_invalid_document_error_embedded():
class Parents(EmbeddedDocument):
father = ReferenceField('Person', reverse_delete_rule=DENY)
mother = ReferenceField('Person', reverse_delete_rule=DENY)
self.assertRaises(InvalidDocumentError, throw_invalid_document_error_embedded)
def test_reverse_delete_rule_cascade_recurs(self):
"""Ensure that a chain of documents is also deleted upon cascaded