Ticket #12806: 12806.patch
File 12806.patch, 3.2 KB (added by , 15 years ago) |
---|
-
django/db/models/sql/query.py
53 53 54 54 def __iter__(self): 55 55 # Always execute a new query for a new iterator. 56 # This could be opt omized with a cache at the expense of RAM.56 # This could be optimized with a cache at the expense of RAM. 57 57 self._execute_query() 58 58 return iter(self.cursor) 59 59 -
django/db/models/query.py
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" -
tests/modeltests/raw_query/tests.py
185 185 self.assertEqual(normal_authors[index], raw_author) 186 186 second_iterations += 1 187 187 188 self.assertEqual(first_iterations, second_iterations) 189 No newline at end of file 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 -
AUTHORS
501 501 Cheng Zhang 502 502 Glenn Maynard <glenn@zewt.org> 503 503 bthomas 504 Bruno Renié <buburno@gmail.com> 504 505 505 506 A big THANK YOU goes to: 506 507 -
docs/topics/db/sql.txt
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