Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #12806 -- Added an implementation of `RawQuerySet.__getitem__`.…

… Thanks, Bruno Renié.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@12504 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit c4699b0b8a8503e552de1fe4f873685f7037b337 1 parent 3498279
Justin Bronn authored February 23, 2010
1  AUTHORS
@@ -503,6 +503,7 @@ answer newbie questions, and generally made Django that much better:
503 503
     Cheng Zhang
504 504
     Glenn Maynard <glenn@zewt.org>
505 505
     bthomas
  506
+    Bruno Renié <buburno@gmail.com>
506 507
 
507 508
 A big THANK YOU goes to:
508 509
 
3  django/db/models/query.py
@@ -1334,6 +1334,9 @@ def __iter__(self):
1334 1334
     def __repr__(self):
1335 1335
         return "<RawQuerySet: %r>" % (self.raw_query % self.params)
1336 1336
 
  1337
+    def __getitem__(self, k):
  1338
+        return list(self)[k]
  1339
+
1337 1340
     @property
1338 1341
     def db(self):
1339 1342
         "Return the database that will be used if this query is executed now"
2  django/db/models/sql/query.py
@@ -54,7 +54,7 @@ def validate_sql(self, sql):
54 54
 
55 55
     def __iter__(self):
56 56
         # Always execute a new query for a new iterator.
57  
-        # This could be optomized with a cache at the expense of RAM.
  57
+        # This could be optimized with a cache at the expense of RAM.
58 58
         self._execute_query()
59 59
         return iter(self.cursor)
60 60
 
14  docs/topics/db/sql.txt
@@ -91,6 +91,20 @@ query could also be written::
91 91
     >>> name_map = {'first': 'first_name', 'last': 'last_name', 'bd': 'birth_date', 'pk': 'id'}
92 92
     >>> Person.objects.raw('SELECT * FROM some_other_table', translations=name_map)
93 93
 
  94
+Index lookups
  95
+-------------
  96
+
  97
+``raw()`` supports indexing, so if you need only the first result you can
  98
+write::
  99
+
  100
+    >>> first_person = Person.objects.raw('SELECT * from myapp_person')[0]
  101
+
  102
+However, the indexing and slicing are not performed at the database level. If
  103
+you have a big amount of ``Person`` objects in your database, it would be more
  104
+efficient to limit the query at the SQL level::
  105
+
  106
+    >>> first_person = Person.objects.raw('SELECT * from myapp_person LIMIT 1')[0]
  107
+
94 108
 Deferring model fields
95 109
 ----------------------
96 110
 
17  tests/modeltests/raw_query/tests.py
@@ -185,4 +185,19 @@ def testMultipleIterations(self):
185 185
             self.assertEqual(normal_authors[index], raw_author)
186 186
             second_iterations += 1
187 187
 
188  
-        self.assertEqual(first_iterations, second_iterations)
  188
+        self.assertEqual(first_iterations, second_iterations)
  189
+
  190
+    def testGetItem(self):
  191
+        # Indexing on RawQuerySets
  192
+        query = "SELECT * FROM raw_query_author ORDER BY id ASC"
  193
+        third_author = Author.objects.raw(query)[2]
  194
+        self.assertEqual(third_author.first_name, 'Bob')
  195
+
  196
+        first_two = Author.objects.raw(query)[0:2]
  197
+        self.assertEquals(len(first_two), 2)
  198
+
  199
+        try:
  200
+            Author.objects.raw(query)['test']
  201
+            self.fail('Index lookups should only accept int, long or slice')
  202
+        except TypeError:
  203
+            pass

0 notes on commit c4699b0

Please sign in to comment.
Something went wrong with that request. Please try again.