Conflicts:
	AUTHORS
This commit is contained in:
kelvinhammond 2013-06-21 07:26:40 -04:00
commit e0d2fab3c3
5 changed files with 74 additions and 10 deletions

View File

@ -171,3 +171,4 @@ that much better:
* Nigel McNie (https://github.com/nigelmcnie)
* ygbourhis (https://github.com/ygbourhis)
* Bob Dickinson (https://github.com/BobDickinson)
* Michael Bartnett (https://github.com/michaelbartnett)

View File

@ -2,6 +2,11 @@
Changelog
=========
Changes in 0.8.3
================
- Fixed weakref being valid after reload (#374)
- Fixed queryset.get() respecting no_dereference (#373)
- Added full_result kwarg to update (#380)
Changes in 0.8.2
================

View File

@ -353,6 +353,12 @@ class Document(BaseDocument):
been saved.
"""
if not self.pk:
if kwargs.get('upsert', False):
query = self.to_mongo()
if "_cls" in query:
del(query["_cls"])
return self._qs.filter(**query).update_one(**kwargs)
else:
raise OperationError('attempt to update a document not yet saved')
# Need to add shard key to query, or you get an error
@ -474,6 +480,7 @@ class Document(BaseDocument):
value = [self._reload(key, v) for v in value]
value = BaseList(value, self, key)
elif isinstance(value, (EmbeddedDocument, DynamicEmbeddedDocument)):
value._instance = None
value._changed_fields = []
return value

View File

@ -245,8 +245,10 @@ class QuerySet(object):
.. versionadded:: 0.3
"""
queryset = self.__call__(*q_objs, **query)
queryset = self.clone()
queryset = queryset.limit(2)
queryset = queryset.filter(*q_objs, **query)
try:
result = queryset.next()
except StopIteration:
@ -472,7 +474,8 @@ class QuerySet(object):
queryset._collection.remove(queryset._query, write_concern=write_concern)
def update(self, upsert=False, multi=True, write_concern=None, **update):
def update(self, upsert=False, multi=True, write_concern=None,
full_result=False, **update):
"""Perform an atomic update on the fields matched by the query.
:param upsert: Any existing document with that "_id" is overwritten.
@ -483,6 +486,8 @@ class QuerySet(object):
``save(..., write_concern={w: 2, fsync: True}, ...)`` will
wait until at least two servers have recorded the write and
will force an fsync on the primary server.
:param full_result: Return the full result rather than just the number
updated.
:param update: Django-style update keyword arguments
.. versionadded:: 0.2
@ -504,12 +509,13 @@ class QuerySet(object):
update["$set"]["_cls"] = queryset._document._class_name
else:
update["$set"] = {"_cls": queryset._document._class_name}
try:
ret = queryset._collection.update(query, update, multi=multi,
result = queryset._collection.update(query, update, multi=multi,
upsert=upsert, **write_concern)
if ret is not None and 'n' in ret:
return ret['n']
if full_result:
return result
elif result:
return result['n']
except pymongo.errors.OperationFailure, err:
if unicode(err) == u'multi not coded yet':
message = u'update() method requires MongoDB 1.1.3+'
@ -1159,8 +1165,8 @@ class QuerySet(object):
raw_doc = self._cursor.next()
if self._as_pymongo:
return self._get_as_pymongo(raw_doc)
doc = self._document._from_son(raw_doc)
doc = self._document._from_son(raw_doc,
_auto_dereference=self._auto_dereference)
if self._scalar:
return self._get_scalar(doc)

View File

@ -536,6 +536,23 @@ class QuerySetTest(unittest.TestCase):
self.assertEqual(club.members['John']['gender'], "F")
self.assertEqual(club.members['John']['age'], 14)
def test_update_results(self):
self.Person.drop_collection()
result = self.Person(name="Bob", age=25).update(upsert=True, full_result=True)
self.assertIsInstance(result, dict)
self.assertTrue("upserted" in result)
self.assertFalse(result["updatedExisting"])
bob = self.Person.objects.first()
result = bob.update(set__age=30, full_result=True)
self.assertIsInstance(result, dict)
self.assertTrue(result["updatedExisting"])
self.Person(name="Bob", age=20).save()
result = self.Person.objects(name="Bob").update(set__name="bobby", multi=True)
self.assertEqual(result, 2)
def test_upsert(self):
self.Person.drop_collection()
@ -1596,6 +1613,32 @@ class QuerySetTest(unittest.TestCase):
self.assertEqual(message.authors[1].name, "Ross")
self.assertEqual(message.authors[2].name, "Adam")
def test_reload_embedded_docs_instance(self):
class SubDoc(EmbeddedDocument):
val = IntField()
class Doc(Document):
embedded = EmbeddedDocumentField(SubDoc)
doc = Doc(embedded=SubDoc(val=0)).save()
doc.reload()
self.assertEqual(doc.pk, doc.embedded._instance.pk)
def test_reload_list_embedded_docs_instance(self):
class SubDoc(EmbeddedDocument):
val = IntField()
class Doc(Document):
embedded = ListField(EmbeddedDocumentField(SubDoc))
doc = Doc(embedded=[SubDoc(val=0)]).save()
doc.reload()
self.assertEqual(doc.pk, doc.embedded[0]._instance.pk)
def test_order_by(self):
"""Ensure that QuerySets may be ordered.
"""
@ -3241,6 +3284,8 @@ class QuerySetTest(unittest.TestCase):
self.assertTrue(isinstance(qs.first().organization, Organization))
self.assertFalse(isinstance(qs.no_dereference().first().organization,
Organization))
self.assertFalse(isinstance(qs.no_dereference().get().organization,
Organization))
self.assertTrue(isinstance(qs.first().organization, Organization))
def test_cached_queryset(self):