Permalink
Browse files

[soc2010/query-refactor] Implemented order_by, also fixed a typo in "…

…The Beatles", sorry.

git-svn-id: http://code.djangoproject.com/svn/django/branches/soc2010/query-refactor@13370 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
1 parent bf071f7 commit 6c6dc8fd7169d43e653be3ca32e1a93a09fa2479 @alex alex committed Jun 21, 2010
Showing with 35 additions and 4 deletions.
  1. +9 −2 django/contrib/mongodb/compiler.py
  2. +26 −2 tests/regressiontests/mongodb/tests.py
View
11 django/contrib/mongodb/compiler.py
@@ -1,3 +1,5 @@
+from pymongo import ASCENDING, DESCENDING
+
from django.db.models.sql.datastructures import FullResultSet
@@ -62,10 +64,15 @@ def build_query(self, aggregates=False):
assert not self.query.extra
assert not self.query.having
assert self.query.high_mark is None
- assert not self.query.order_by
filters = self.get_filters(self.query.where)
- return self.connection.db[self.query.model._meta.db_table].find(filters)
+ cursor = self.connection.db[self.query.model._meta.db_table].find(filters)
+ if self.query.order_by:
+ cursor = cursor.sort([
+ (ordering.lstrip("-"), DESCENDING if ordering.startswith("-") else ASCENDING)
+ for ordering in self.query.order_by
+ ])
+ return cursor
def results_iter(self):
query = self.build_query()
View
28 tests/regressiontests/mongodb/tests.py
@@ -63,15 +63,39 @@ def test_exists(self):
Artist.objects.create(name="Brian May")
self.assertTrue(Artist.objects.filter(name="Brian May").exists())
+ def test_orderby(self):
+ Group.objects.create(name="Queen", year_formed=1971)
+ Group.objects.create(name="The E Street Band", year_formed=1972)
+ Group.objects.create(name="The Beatles", year_formed=1960)
+
+ self.assertQuerysetEqual(
+ Group.objects.order_by("year_formed"), [
+ "The Beatles",
+ "Queen",
+ "The E Street Band",
+ ],
+ lambda g: g.name
+ )
+
+ self.assertQuerysetEqual(
+ Group.objects.order_by("-year_formed"), [
+ "The E Street Band",
+ "Queen",
+ "The Beatles",
+ ],
+ lambda g: g.name,
+ )
+
+
def test_not_equals(self):
q = Group.objects.create(name="Queen", year_formed=1971)
e = Group.objects.create(name="The E Street Band", year_formed=1972)
- b = Group.objects.create(name="The Beetles")
+ b = Group.objects.create(name="The Beatles")
self.assertQuerysetEqual(
Group.objects.exclude(year_formed=1972), [
"Queen",
- "The Beetles",
+ "The Beatles",
],
lambda g: g.name,
)

0 comments on commit 6c6dc8f

Please sign in to comment.