Updates can now take raw queries
This commit is contained in:
		| @@ -1279,6 +1279,9 @@ class QuerySet(object): | |||||||
|  |  | ||||||
|         mongo_update = {} |         mongo_update = {} | ||||||
|         for key, value in update.items(): |         for key, value in update.items(): | ||||||
|  |             if key == "__raw__": | ||||||
|  |                 mongo_update.update(value) | ||||||
|  |                 continue | ||||||
|             parts = key.split('__') |             parts = key.split('__') | ||||||
|             # Check for an operator and transform to mongo-style if there is |             # Check for an operator and transform to mongo-style if there is | ||||||
|             op = None |             op = None | ||||||
|   | |||||||
| @@ -1420,20 +1420,39 @@ class QuerySetTest(unittest.TestCase): | |||||||
|  |  | ||||||
|         BlogPost.drop_collection() |         BlogPost.drop_collection() | ||||||
|  |  | ||||||
|     def test_update_pull(self): |     def test_update_push_and_pull(self): | ||||||
|         """Ensure that the 'pull' update operation works correctly. |         """Ensure that the 'pull' update operation works correctly. | ||||||
|         """ |         """ | ||||||
|         class BlogPost(Document): |         class BlogPost(Document): | ||||||
|             slug = StringField() |             slug = StringField() | ||||||
|             tags = ListField(StringField()) |             tags = ListField(StringField()) | ||||||
|  |  | ||||||
|         post = BlogPost(slug="test", tags=['code', 'mongodb', 'code']) |         BlogPost.drop_collection() | ||||||
|  |  | ||||||
|  |         post = BlogPost(slug="test") | ||||||
|         post.save() |         post.save() | ||||||
|  |  | ||||||
|  |         BlogPost.objects.filter(id=post.id).update(push__tags="code") | ||||||
|  |         post.reload() | ||||||
|  |         self.assertEqual(post.tags, ["code"]) | ||||||
|  |  | ||||||
|  |         BlogPost.objects.filter(id=post.id).update(push_all__tags=["mongodb", "code"]) | ||||||
|  |         post.reload() | ||||||
|  |         self.assertEqual(post.tags, ["code", "mongodb", "code"]) | ||||||
|  |  | ||||||
|         BlogPost.objects(slug="test").update(pull__tags="code") |         BlogPost.objects(slug="test").update(pull__tags="code") | ||||||
|         post.reload() |         post.reload() | ||||||
|         self.assertTrue('code' not in post.tags) |         self.assertEqual(post.tags, ["mongodb"]) | ||||||
|         self.assertEqual(len(post.tags), 1) |  | ||||||
|  |  | ||||||
|  |         BlogPost.objects(slug="test").update(pull_all__tags=["mongodb", "code"]) | ||||||
|  |         post.reload() | ||||||
|  |         self.assertEqual(post.tags, []) | ||||||
|  |  | ||||||
|  |         BlogPost.objects(slug="test").update(__raw__={"$addToSet": {"tags": {"$each": ["code", "mongodb", "code"]}}}) | ||||||
|  |         post.reload() | ||||||
|  |         self.assertEqual(post.tags, ["code", "mongodb"]) | ||||||
|  |  | ||||||
|  |  | ||||||
|     def test_update_one_pop_generic_reference(self): |     def test_update_one_pop_generic_reference(self): | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user