Django

Code

Changeset 7149

Show
Ignore:
Timestamp:
02/22/08 21:36:38 (5 months ago)
Author:
mtredinnick
Message:

queryset-refactor: Added valuelist() method to querysets. Refs #2482.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/branches/queryset-refactor/django/db/models/manager.py

    r7148 r7149  
    102102        return self.get_query_set().values(*args, **kwargs) 
    103103 
     104    def valueslist(self, *args, **kwargs): 
     105        return self.get_query_set().valueslist(*args, **kwargs) 
     106 
    104107    def update(self, *args, **kwargs): 
    105108        return self.get_query_set().update(*args, **kwargs) 
  • django/branches/queryset-refactor/django/db/models/query.py

    r7148 r7149  
    278278    def values(self, *fields): 
    279279        return self._clone(klass=ValuesQuerySet, setup=True, _fields=fields) 
     280 
     281    def valueslist(self, *fields, **kwargs): 
     282        flat = kwargs.pop('flat', False) 
     283        if kwargs: 
     284            raise TypeError('Unexpected keyword arguments to valueslist: %s' 
     285                    % (kwargs.keys(),)) 
     286        if flat and len(fields) > 1: 
     287            raise TypeError("'flat' is not valid when valueslist is called with more than one field.") 
     288        return self._clone(klass=ValuesListQuerySet, setup=True, flat=flat, 
     289                _fields=fields) 
    280290 
    281291    def dates(self, field_name, kind, order='ASC'): 
     
    532542        return c 
    533543 
     544class ValuesListQuerySet(ValuesQuerySet): 
     545    def iterator(self): 
     546        self.field_names.extend([f for f in self.query.extra_select.keys()]) 
     547        if self.flat and len(self._fields) == 1: 
     548            for row in self.query.results_iter(): 
     549                yield row[0] 
     550        else: 
     551            for row in self.query.results_iter(): 
     552                yield row 
     553 
     554    def _clone(self, *args, **kwargs): 
     555        clone = super(ValuesListQuerySet, self)._clone(*args, **kwargs) 
     556        clone.flat = self.flat 
     557        return clone 
     558 
    534559class DateQuerySet(QuerySet): 
    535560    def iterator(self): 
  • django/branches/queryset-refactor/docs/db-api.txt

    r7148 r7149  
    665665individualism. 
    666666 
     667``valueslist(*fields)`` 
     668~~~~~~~~~~~~~~~~~~~~~~~ 
     669 
     670**New in Django development version** 
     671 
     672This is similar to ``values()`` except that instead of returning a list of 
     673dictionaries, it returns a list of tuples. Each tuple contains the value from 
     674the respective field passed into the ``valueslist()`` call -- so the first 
     675item is the first field, etc. For example:: 
     676 
     677    >>> Entry.objects.valueslist('id', 'headling') 
     678    [(1, u'First entry'), ...] 
     679 
     680If you only pass in a single field, you can also pass in the ``flat`` 
     681parameter. If ``True``, this will mean the returned results are single values, 
     682rather than one-tuples. An example should make the difference clearer:: 
     683 
     684    >>> Entry.objects.valueslist('id').order_by('id') 
     685    [(1,), (2,), (3,), ...] 
     686 
     687    >>> Entry.objects.valueslist('id', flat=True).order_by('id') 
     688    [1, 2, 3, ...] 
     689 
     690It is an error to pass in ``flat`` when there is more than one field. 
     691 
     692If you don't pass any values to ``valueslist()``, it will return all the 
     693fields in the model, in the order they were declared. 
     694 
    667695``dates(field, kind, order='ASC')`` 
    668696~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
  • django/branches/queryset-refactor/tests/modeltests/lookup/models.py

    r6730 r7149  
    166166True 
    167167 
     168# valueslist() is similar to values(), except that the results are returned as 
     169# a list of tuples, rather than a list of dictionaries. Within each tuple, the 
     170# order of the elemnts is the same as the order of fields in the valueslist() 
     171# call. 
     172>>> Article.objects.valueslist('headline') 
     173[(u'Article 5',), (u'Article 6',), (u'Article 4',), (u'Article 2',), (u'Article 3',), (u'Article 7',), (u'Article 1',)] 
     174 
     175>>> Article.objects.valueslist('id').order_by('id') 
     176[(1,), (2,), (3,), (4,), (5,), (6,), (7,)] 
     177>>> Article.objects.valueslist('id', flat=True).order_by('id') 
     178[1, 2, 3, 4, 5, 6, 7] 
     179 
     180>>> Article.objects.valueslist('id', 'headline', flat=True) 
     181Traceback (most recent call last): 
     182... 
     183TypeError: 'flat' is not valid when valueslist is called with more than one field. 
     184 
    168185# Every DateField and DateTimeField creates get_next_by_FOO() and 
    169186# get_previous_by_FOO() methods.