Ticket #12806: 12806.patch

File 12806.patch, 3.2 KB (added by Bruno Renié, 14 years ago)

Patch adds indexing w/ tests and a mention in the docs

  • django/db/models/sql/query.py

     
    5353
    5454    def __iter__(self):
    5555        # Always execute a new query for a new iterator.
    56         # This could be optomized with a cache at the expense of RAM.
     56        # This could be optimized with a cache at the expense of RAM.
    5757        self._execute_query()
    5858        return iter(self.cursor)
    5959
  • django/db/models/query.py

     
    13341334    def __repr__(self):
    13351335        return "<RawQuerySet: %r>" % (self.raw_query % self.params)
    13361336
     1337    def __getitem__(self, k):
     1338        return list(self)[k]
     1339
    13371340    @property
    13381341    def db(self):
    13391342        "Return the database that will be used if this query is executed now"
  • tests/modeltests/raw_query/tests.py

     
    185185            self.assertEqual(normal_authors[index], raw_author)
    186186            second_iterations += 1
    187187
    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

     
    501501    Cheng Zhang
    502502    Glenn Maynard <glenn@zewt.org>
    503503    bthomas
     504    Bruno Renié <buburno@gmail.com>
    504505
    505506A big THANK YOU goes to:
    506507
  • docs/topics/db/sql.txt

     
    9191    >>> name_map = {'first': 'first_name', 'last': 'last_name', 'bd': 'birth_date', 'pk': 'id'}
    9292    >>> Person.objects.raw('SELECT * FROM some_other_table', translations=name_map)
    9393
     94Index lookups
     95-------------
     96
     97``raw()`` supports indexing, so if you need only the first result you can
     98write::
     99
     100    >>> first_person = Person.objects.raw('SELECT * from myapp_person')[0]
     101
     102However, the indexing and slicing are not performed at the database level. If
     103you have a big amount of ``Person`` objects in your database, it would be more
     104efficient to limit the query at the SQL level::
     105
     106    >>> first_person = Person.objects.raw('SELECT * from myapp_person LIMIT 1')[0]
     107
    94108Deferring model fields
    95109----------------------
    96110
Back to Top