Skip to content

Commit d0d93a9

Browse files
committed
[Bugfix]: Maintain key order within _make_result iter
1 parent 64b1c10 commit d0d93a9

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

django_mongodb_backend/queryset.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,4 +93,4 @@ def _make_result(self, query):
9393
of __iter__().
9494
"""
9595
for result in query:
96-
yield tuple(result.values())
96+
yield tuple(result.get(key, None) for key in self.queryset.columns)

tests/raw_query_/test_raw_aggregate.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
from datetime import date
66

7+
from django.db import connection
78
from django.core.exceptions import FieldDoesNotExist
89
from django.test import TestCase
910

@@ -170,6 +171,30 @@ def test_order_handler(self):
170171
authors = Author.objects.all()
171172
self.assertSuccessfulRawQuery(Author, query, authors)
172173

174+
def test_different_ordered_in_database(self):
175+
"""Documents in MongoDB are not required to maintain key order as
176+
a means to improve write efficiency. Documents can be returned
177+
to Django out of order. This can lead to incorrect information being placed
178+
in a RawQueryset object.
179+
"""
180+
database = connection["<TEST_DATABASE>"].database
181+
raw_insert = Author(first_name="Out of", last_name="Order", dob=date(1950, 9, 20))
182+
try:
183+
# Insert a document into the database in reverse
184+
database[Author._meta.db_table].insert_one(
185+
{
186+
field.name: getattr(field.name, raw_insert)
187+
for field in reversed(Author._meta.get_fields())
188+
}
189+
)
190+
query = []
191+
authors = Author.objects.all()
192+
self.assertSuccessfulRawQuery(Author, query, authors)
193+
finally:
194+
database[Author._meta.db_table].delete_one(
195+
{"first_name": raw_insert.first_name, "last_name": raw_insert.last_name}
196+
)
197+
173198
def test_query_representation(self):
174199
"""Test representation of raw query."""
175200
query = [{"$match": {"last_name": "foo"}}]

0 commit comments

Comments
 (0)