Conflicts:
	mongoengine/base.py
This commit is contained in:
Ross Lawley 2012-09-03 11:00:41 +01:00
commit 52f85aab18
5 changed files with 28 additions and 7 deletions

View File

@ -122,3 +122,4 @@ that much better:
* Sergey Nikitin * Sergey Nikitin
* psychogenic * psychogenic
* Stefan Wójcik * Stefan Wójcik
* dimonb

View File

@ -4,6 +4,7 @@ Changelog
Changes in 0.7.X Changes in 0.7.X
================= =================
- Fixed reloading on sharded documents (hmarr/mongoengine#569)
- Added NotUniqueError for duplicate keys (MongoEngine/mongoengine#62) - Added NotUniqueError for duplicate keys (MongoEngine/mongoengine#62)
- Added custom collection / sequence naming for SequenceFields (MongoEngine/mongoengine#92) - Added custom collection / sequence naming for SequenceFields (MongoEngine/mongoengine#92)
- Fixed UnboundLocalError in composite index with pk field (MongoEngine/mongoengine#88) - Fixed UnboundLocalError in composite index with pk field (MongoEngine/mongoengine#88)

View File

@ -949,8 +949,10 @@ class BaseDocument(object):
self._data[name] = value self._data[name] = value
if hasattr(self, '_changed_fields'): if hasattr(self, '_changed_fields'):
self._mark_as_changed(name) self._mark_as_changed(name)
if (self._is_document and not self._created and if (self._is_document and not self._created and
name in self._meta.get('shard_key', tuple())): name in self._meta.get('shard_key', tuple()) and
self._data.get(name) != value):
OperationError = _import_class('OperationError') OperationError = _import_class('OperationError')
msg = "Shard Keys are immutable. Tried to update %s" % name msg = "Shard Keys are immutable. Tried to update %s" % name
raise OperationError(msg) raise OperationError(msg)

View File

@ -295,6 +295,16 @@ class Document(BaseDocument):
ref.save(**kwargs) ref.save(**kwargs)
ref._changed_fields = [] ref._changed_fields = []
@property
def _object_key(self):
"""Dict to identify object in collection
"""
select_dict = {'pk': self.pk}
shard_key = self.__class__._meta.get('shard_key', tuple())
for k in shard_key:
select_dict[k] = getattr(self, k)
return select_dict
def update(self, **kwargs): def update(self, **kwargs):
"""Performs an update on the :class:`~mongoengine.Document` """Performs an update on the :class:`~mongoengine.Document`
A convenience wrapper to :meth:`~mongoengine.QuerySet.update`. A convenience wrapper to :meth:`~mongoengine.QuerySet.update`.
@ -306,11 +316,7 @@ class Document(BaseDocument):
raise OperationError('attempt to update a document not yet saved') raise OperationError('attempt to update a document not yet saved')
# Need to add shard key to query, or you get an error # Need to add shard key to query, or you get an error
select_dict = {'pk': self.pk} return self.__class__.objects(**self._object_key).update_one(**kwargs)
shard_key = self.__class__._meta.get('shard_key', tuple())
for k in shard_key:
select_dict[k] = getattr(self, k)
return self.__class__.objects(**select_dict).update_one(**kwargs)
def delete(self, safe=False): def delete(self, safe=False):
"""Delete the :class:`~mongoengine.Document` from the database. This """Delete the :class:`~mongoengine.Document` from the database. This
@ -321,7 +327,7 @@ class Document(BaseDocument):
signals.pre_delete.send(self.__class__, document=self) signals.pre_delete.send(self.__class__, document=self)
try: try:
self.__class__.objects(pk=self.pk).delete(safe=safe) self.__class__.objects(**self._object_key).delete(safe=safe)
except pymongo.errors.OperationFailure, err: except pymongo.errors.OperationFailure, err:
message = u'Could not delete document (%s)' % err.message message = u'Could not delete document (%s)' % err.message
raise OperationError(message) raise OperationError(message)

View File

@ -1258,6 +1258,17 @@ class DocumentTest(unittest.TestCase):
self.assertEqual(person.name, "Mr Test User") self.assertEqual(person.name, "Mr Test User")
self.assertEqual(person.age, 21) self.assertEqual(person.age, 21)
def test_reload_sharded(self):
class Animal(Document):
superphylum = StringField()
meta = {'shard_key': ('superphylum',)}
Animal.drop_collection()
doc = Animal(superphylum = 'Deuterostomia')
doc.save()
doc.reload()
Animal.drop_collection()
def test_reload_referencing(self): def test_reload_referencing(self):
"""Ensures reloading updates weakrefs correctly """Ensures reloading updates weakrefs correctly
""" """