Changeset 7451
- Timestamp:
- 04/24/08 06:11:30 (2 months ago)
- Files:
-
- django/branches/queryset-refactor/django/db/models/manager.py (modified) (1 diff)
- django/branches/queryset-refactor/django/db/models/query.py (modified) (1 diff)
- django/branches/queryset-refactor/docs/db-api.txt (modified) (3 diffs)
- django/branches/queryset-refactor/tests/modeltests/lookup/models.py (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
django/branches/queryset-refactor/django/db/models/manager.py
r7437 r7451 115 115 return self.get_query_set().values(*args, **kwargs) 116 116 117 def values list(self, *args, **kwargs):118 return self.get_query_set().values list(*args, **kwargs)117 def values_list(self, *args, **kwargs): 118 return self.get_query_set().values_list(*args, **kwargs) 119 119 120 120 def update(self, *args, **kwargs): django/branches/queryset-refactor/django/db/models/query.py
r7446 r7451 308 308 return self._clone(klass=ValuesQuerySet, setup=True, _fields=fields) 309 309 310 def values list(self, *fields, **kwargs):310 def values_list(self, *fields, **kwargs): 311 311 flat = kwargs.pop('flat', False) 312 312 if kwargs: 313 raise TypeError('Unexpected keyword arguments to values list: %s'313 raise TypeError('Unexpected keyword arguments to values_list: %s' 314 314 % (kwargs.keys(),)) 315 315 if flat and len(fields) > 1: 316 raise TypeError("'flat' is not valid when values list is called with more than one field.")316 raise TypeError("'flat' is not valid when values_list is called with more than one field.") 317 317 return self._clone(klass=ValuesListQuerySet, setup=True, flat=flat, 318 318 _fields=fields) django/branches/queryset-refactor/docs/db-api.txt
r7448 r7451 649 649 individualism. 650 650 651 ``values list(*fields)``651 ``values_list(*fields)`` 652 652 ~~~~~~~~~~~~~~~~~~~~~~~ 653 653 … … 656 656 This is similar to ``values()`` except that instead of returning a list of 657 657 dictionaries, it returns a list of tuples. Each tuple contains the value from 658 the respective field passed into the ``values list()`` call -- so the first658 the respective field passed into the ``values_list()`` call -- so the first 659 659 item is the first field, etc. For example:: 660 660 661 >>> Entry.objects.values list('id', 'headling')661 >>> Entry.objects.values_list('id', 'headling') 662 662 [(1, u'First entry'), ...] 663 663 … … 666 666 rather than one-tuples. An example should make the difference clearer:: 667 667 668 >>> Entry.objects.values list('id').order_by('id')668 >>> Entry.objects.values_list('id').order_by('id') 669 669 [(1,), (2,), (3,), ...] 670 670 671 >>> Entry.objects.values list('id', flat=True).order_by('id')671 >>> Entry.objects.values_list('id', flat=True).order_by('id') 672 672 [1, 2, 3, ...] 673 673 674 674 It is an error to pass in ``flat`` when there is more than one field. 675 675 676 If you don't pass any values to ``values list()``, it will return all the676 If you don't pass any values to ``values_list()``, it will return all the 677 677 fields in the model, in the order they were declared. 678 678 django/branches/queryset-refactor/tests/modeltests/lookup/models.py
r7446 r7451 169 169 True 170 170 171 # values list() is similar to values(), except that the results are returned as171 # values_list() is similar to values(), except that the results are returned as 172 172 # a list of tuples, rather than a list of dictionaries. Within each tuple, the 173 # order of the elemnts is the same as the order of fields in the values list()173 # order of the elemnts is the same as the order of fields in the values_list() 174 174 # call. 175 >>> Article.objects.values list('headline')175 >>> Article.objects.values_list('headline') 176 176 [(u'Article 5',), (u'Article 6',), (u'Article 4',), (u'Article 2',), (u'Article 3',), (u'Article 7',), (u'Article 1',)] 177 177 178 >>> Article.objects.values list('id').order_by('id')178 >>> Article.objects.values_list('id').order_by('id') 179 179 [(1,), (2,), (3,), (4,), (5,), (6,), (7,)] 180 >>> Article.objects.values list('id', flat=True).order_by('id')180 >>> Article.objects.values_list('id', flat=True).order_by('id') 181 181 [1, 2, 3, 4, 5, 6, 7] 182 182 183 >>> Article.objects.extra(select={'id_plus_one': 'id+1'}).order_by('id').values list('id')183 >>> Article.objects.extra(select={'id_plus_one': 'id+1'}).order_by('id').values_list('id') 184 184 [(1,), (2,), (3,), (4,), (5,), (6,), (7,)] 185 >>> Article.objects.extra(select={'id_plus_one': 'id+1'}).order_by('id').values list('id_plus_one', 'id')185 >>> Article.objects.extra(select={'id_plus_one': 'id+1'}).order_by('id').values_list('id_plus_one', 'id') 186 186 [(2, 1), (3, 2), (4, 3), (5, 4), (6, 5), (7, 6), (8, 7)] 187 >>> Article.objects.extra(select={'id_plus_one': 'id+1'}).order_by('id').values list('id', 'id_plus_one')187 >>> Article.objects.extra(select={'id_plus_one': 'id+1'}).order_by('id').values_list('id', 'id_plus_one') 188 188 [(1, 2), (2, 3), (3, 4), (4, 5), (5, 6), (6, 7), (7, 8)] 189 189 190 >>> Article.objects.values list('id', 'headline', flat=True)190 >>> Article.objects.values_list('id', 'headline', flat=True) 191 191 Traceback (most recent call last): 192 192 ... 193 TypeError: 'flat' is not valid when values list is called with more than one field.193 TypeError: 'flat' is not valid when values_list is called with more than one field. 194 194 195 195 # Every DateField and DateTimeField creates get_next_by_FOO() and
