Loading AUTHORS +1 −0 Original line number Diff line number Diff line Loading @@ -503,6 +503,7 @@ answer newbie questions, and generally made Django that much better: Cheng Zhang Glenn Maynard <glenn@zewt.org> bthomas Bruno Renié <buburno@gmail.com> A big THANK YOU goes to: Loading django/db/models/query.py +3 −0 Original line number Diff line number Diff line Loading @@ -1334,6 +1334,9 @@ class RawQuerySet(object): def __repr__(self): return "<RawQuerySet: %r>" % (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" Loading django/db/models/sql/query.py +1 −1 Original line number Diff line number Diff line Loading @@ -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) Loading docs/topics/db/sql.txt +14 −0 Original line number Diff line number Diff line Loading @@ -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 ---------------------- Loading tests/modeltests/raw_query/tests.py +16 −1 Original line number Diff line number Diff line Loading @@ -186,3 +186,18 @@ class RawQueryTests(TestCase): second_iterations += 1 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 Loading
AUTHORS +1 −0 Original line number Diff line number Diff line Loading @@ -503,6 +503,7 @@ answer newbie questions, and generally made Django that much better: Cheng Zhang Glenn Maynard <glenn@zewt.org> bthomas Bruno Renié <buburno@gmail.com> A big THANK YOU goes to: Loading
django/db/models/query.py +3 −0 Original line number Diff line number Diff line Loading @@ -1334,6 +1334,9 @@ class RawQuerySet(object): def __repr__(self): return "<RawQuerySet: %r>" % (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" Loading
django/db/models/sql/query.py +1 −1 Original line number Diff line number Diff line Loading @@ -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) Loading
docs/topics/db/sql.txt +14 −0 Original line number Diff line number Diff line Loading @@ -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 ---------------------- Loading
tests/modeltests/raw_query/tests.py +16 −1 Original line number Diff line number Diff line Loading @@ -186,3 +186,18 @@ class RawQueryTests(TestCase): second_iterations += 1 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