Skip to content

Commit 9bbd8db

Browse files
committed
Querysets now return clones and are no longer edit in place
Fixes #56
1 parent 09a5f5c commit 9bbd8db

File tree

6 files changed

+646
-560
lines changed

6 files changed

+646
-560
lines changed

docs/changelog.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ Changes in 0.8.X
2626
- Fix Django timezone support (#151)
2727
- Simplified Q objects, removed QueryTreeTransformerVisitor (#98) (#171)
2828
- FileFields now copyable (#198)
29+
- Querysets now return clones and are no longer edit in place (#56)
2930

3031
Changes in 0.7.9
3132
================

docs/upgrade.rst

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,28 @@ you will need to declare :attr:`allow_inheritance` in the meta data like so: ::
5656

5757
meta = {'allow_inheritance': True}
5858

59+
Querysets
60+
~~~~~~~~~
61+
62+
Querysets now return clones and should no longer be considered editable in
63+
place. This brings us in line with how Django's querysets work and removes a
64+
long running gotcha. If you edit your querysets inplace you will have to
65+
update your code like so: ::
66+
67+
# Old code:
68+
mammals = Animal.objects(type="mammal")
69+
mammals.filter(order="Carnivora") # Returns a cloned queryset that isn't assigned to anything - so this will break in 0.8
70+
[m for m in mammals] # This will return all mammals in 0.8 as the 2nd filter returned a new queryset
71+
72+
# Update example a) assign queryset after a change:
73+
mammals = Animal.objects(type="mammal")
74+
carnivores = mammals.filter(order="Carnivora") # Reassign the new queryset so fitler can be applied
75+
[m for m in carnivores] # This will return all carnivores
76+
77+
# Update example b) chain the queryset:
78+
mammals = Animal.objects(type="mammal").filter(order="Carnivora") # The final queryset is assgined to mammals
79+
[m for m in mammals] # This will return all carnivores
80+
5981
Indexes
6082
-------
6183

mongoengine/connection.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,10 @@ def register_connection(alias, name, host='localhost', port=27017,
2828
:param name: the name of the specific database to use
2929
:param host: the host name of the :program:`mongod` instance to connect to
3030
:param port: the port that the :program:`mongod` instance is running on
31-
:param is_slave: whether the connection can act as a slave ** Depreciated pymongo 2.0.1+
32-
:param read_preference: The read preference for the collection ** Added pymongo 2.1
31+
:param is_slave: whether the connection can act as a slave
32+
** Depreciated pymongo 2.0.1+
33+
:param read_preference: The read preference for the collection
34+
** Added pymongo 2.1
3335
:param slaves: a list of aliases of slave connections; each of these must
3436
be a registered connection that has :attr:`is_slave` set to ``True``
3537
:param username: username to authenticate with

0 commit comments

Comments
 (0)