Conflicts:
	docs/changelog.rst
This commit is contained in:
Ross Lawley 2012-06-18 21:10:14 +01:00
commit 766d54795f
6 changed files with 44 additions and 2 deletions

View File

@ -105,4 +105,5 @@ that much better:
* Adam Reeve * Adam Reeve
* Anthony Nemitz * Anthony Nemitz
* deignacio * deignacio
* shaunduncan * shaunduncan
* Meir Kriheli

View File

@ -4,6 +4,7 @@ Changelog
Changes in 0.6.X Changes in 0.6.X
================ ================
- Added PULL reverse_delete_rule
- Fixed CASCADE delete bug - Fixed CASCADE delete bug
- Fixed db_field data load error - Fixed db_field data load error
- Fixed recursive save with FileField - Fixed recursive save with FileField

View File

@ -289,6 +289,10 @@ Its value can take any of the following constants:
:const:`mongoengine.CASCADE` :const:`mongoengine.CASCADE`
Any object containing fields that are refererring to the object being deleted Any object containing fields that are refererring to the object being deleted
are deleted first. are deleted first.
:const:`mongoengine.PULL`
Removes the reference to the object (using MongoDB's "pull" operation)
from any object's fields of
:class:`~mongoengine.ListField` (:class:`~mongoengine.ReferenceField`).
.. warning:: .. warning::

View File

@ -656,6 +656,7 @@ class ReferenceField(BaseField):
* NULLIFY - Updates the reference to null. * NULLIFY - Updates the reference to null.
* CASCADE - Deletes the documents associated with the reference. * CASCADE - Deletes the documents associated with the reference.
* DENY - Prevent the deletion of the reference object. * DENY - Prevent the deletion of the reference object.
* PULL - Pull the reference from a :class:`~mongoengine.ListField` of references
Alternative syntax for registering delete rules (useful when implementing Alternative syntax for registering delete rules (useful when implementing
bi-directional delete rules) bi-directional delete rules)

View File

@ -10,7 +10,7 @@ from bson.code import Code
from mongoengine import signals from mongoengine import signals
__all__ = ['queryset_manager', 'Q', 'InvalidQueryError', __all__ = ['queryset_manager', 'Q', 'InvalidQueryError',
'DO_NOTHING', 'NULLIFY', 'CASCADE', 'DENY'] 'DO_NOTHING', 'NULLIFY', 'CASCADE', 'DENY', 'PULL']
# The maximum number of items to display in a QuerySet.__repr__ # The maximum number of items to display in a QuerySet.__repr__
@ -21,6 +21,7 @@ DO_NOTHING = 0
NULLIFY = 1 NULLIFY = 1
CASCADE = 2 CASCADE = 2
DENY = 3 DENY = 3
PULL = 4
class DoesNotExist(Exception): class DoesNotExist(Exception):
@ -1321,6 +1322,10 @@ class QuerySet(object):
document_cls.objects(**{field_name + '__in': self}).update( document_cls.objects(**{field_name + '__in': self}).update(
safe_update=safe, safe_update=safe,
**{'unset__%s' % field_name: 1}) **{'unset__%s' % field_name: 1})
elif rule == PULL:
document_cls.objects(**{field_name + '__in': self}).update(
safe_update=safe,
**{'pull_all__%s' % field_name: self})
self._collection.remove(self._query, safe=safe) self._collection.remove(self._query, safe=safe)

View File

@ -1419,6 +1419,36 @@ class QuerySetTest(unittest.TestCase):
self.assertRaises(OperationError, self.Person.objects.delete) self.assertRaises(OperationError, self.Person.objects.delete)
def test_reverse_delete_rule_pull(self):
"""Ensure pulling of references to deleted documents.
"""
class BlogPost(Document):
content = StringField()
authors = ListField(ReferenceField(self.Person,
reverse_delete_rule=PULL))
BlogPost.drop_collection()
self.Person.drop_collection()
me = self.Person(name='Test User')
me.save()
someoneelse = self.Person(name='Some-one Else')
someoneelse.save()
post = BlogPost(content='Watching TV', authors=[me, someoneelse])
post.save()
another = BlogPost(content='Chilling Out', authors=[someoneelse])
another.save()
someoneelse.delete()
post.reload()
another.reload()
self.assertEqual(post.authors, [me])
self.assertEqual(another.authors, [])
def test_update(self): def test_update(self):
"""Ensure that atomic updates work properly. """Ensure that atomic updates work properly.
""" """