Commits

Anonymous committed 83d3805

[soc2010/query-refactor] Implemented order_by, also fixed a typo in "The Beatles", sorry.

Comments (0)

Files changed (2)

django/contrib/mongodb/compiler.py

+from pymongo import ASCENDING, DESCENDING
+
 from django.db.models.sql.datastructures import FullResultSet
 
 
         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()

tests/regressiontests/mongodb/tests.py

         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,
         )