From c4699b0b8a8503e552de1fe4f873685f7037b337 Mon Sep 17 00:00:00 2001 From: Justin Bronn Date: Tue, 23 Feb 2010 05:22:12 +0000 Subject: [PATCH] =?UTF-8?q?Fixed=20#12806=20--=20Added=20an=20implementati?= =?UTF-8?q?on=20of=20`RawQuerySet.=5F=5Fgetitem=5F=5F`.=20=20Thanks,=20Bru?= =?UTF-8?q?no=20Reni=C3=A9.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit git-svn-id: http://code.djangoproject.com/svn/django/trunk@12504 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- AUTHORS | 1 + django/db/models/query.py | 3 +++ django/db/models/sql/query.py | 2 +- docs/topics/db/sql.txt | 14 ++++++++++++++ tests/modeltests/raw_query/tests.py | 17 ++++++++++++++++- 5 files changed, 35 insertions(+), 2 deletions(-) diff --git a/AUTHORS b/AUTHORS index 93cf2bdeb0..d60a9f72a5 100644 --- a/AUTHORS +++ b/AUTHORS @@ -503,6 +503,7 @@ answer newbie questions, and generally made Django that much better: Cheng Zhang Glenn Maynard bthomas + Bruno ReniƩ A big THANK YOU goes to: diff --git a/django/db/models/query.py b/django/db/models/query.py index 8cb3dbecfc..0424569e0b 100644 --- a/django/db/models/query.py +++ b/django/db/models/query.py @@ -1334,6 +1334,9 @@ class RawQuerySet(object): def __repr__(self): return "" % (self.raw_query % self.params) + def __getitem__(self, k): + return list(self)[k] + @property def db(self): "Return the database that will be used if this query is executed now" diff --git a/django/db/models/sql/query.py b/django/db/models/sql/query.py index 2d3f6109a0..7b1695e051 100644 --- a/django/db/models/sql/query.py +++ b/django/db/models/sql/query.py @@ -54,7 +54,7 @@ class RawQuery(object): def __iter__(self): # Always execute a new query for a new iterator. - # This could be optomized with a cache at the expense of RAM. + # This could be optimized with a cache at the expense of RAM. self._execute_query() return iter(self.cursor) diff --git a/docs/topics/db/sql.txt b/docs/topics/db/sql.txt index 075b9b27a3..1433fa52a1 100644 --- a/docs/topics/db/sql.txt +++ b/docs/topics/db/sql.txt @@ -91,6 +91,20 @@ query could also be written:: >>> name_map = {'first': 'first_name', 'last': 'last_name', 'bd': 'birth_date', 'pk': 'id'} >>> Person.objects.raw('SELECT * FROM some_other_table', translations=name_map) +Index lookups +------------- + +``raw()`` supports indexing, so if you need only the first result you can +write:: + + >>> first_person = Person.objects.raw('SELECT * from myapp_person')[0] + +However, the indexing and slicing are not performed at the database level. If +you have a big amount of ``Person`` objects in your database, it would be more +efficient to limit the query at the SQL level:: + + >>> first_person = Person.objects.raw('SELECT * from myapp_person LIMIT 1')[0] + Deferring model fields ---------------------- diff --git a/tests/modeltests/raw_query/tests.py b/tests/modeltests/raw_query/tests.py index 688df21598..dbc5f88bb9 100644 --- a/tests/modeltests/raw_query/tests.py +++ b/tests/modeltests/raw_query/tests.py @@ -185,4 +185,19 @@ class RawQueryTests(TestCase): self.assertEqual(normal_authors[index], raw_author) second_iterations += 1 - self.assertEqual(first_iterations, second_iterations) \ No newline at end of file + self.assertEqual(first_iterations, second_iterations) + + def testGetItem(self): + # Indexing on RawQuerySets + query = "SELECT * FROM raw_query_author ORDER BY id ASC" + third_author = Author.objects.raw(query)[2] + self.assertEqual(third_author.first_name, 'Bob') + + first_two = Author.objects.raw(query)[0:2] + self.assertEquals(len(first_two), 2) + + try: + Author.objects.raw(query)['test'] + self.fail('Index lookups should only accept int, long or slice') + except TypeError: + pass