Changeset 5767
- Timestamp:
- 07/27/07 16:53:02 (1 year ago)
- Files:
-
- django/trunk/tests/modeltests/lookup/models.py (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
django/trunk/tests/modeltests/lookup/models.py
r5609 r5767 133 133 [('headline', u'Article 1'), ('id', 1)] 134 134 135 136 # you can use values() even on extra fields 137 >>> for d in Article.objects.extra( select={'id_plus_one' : 'id + 1'} ).values('id', 'id_plus_one'): 135 # The values() method works with "extra" fields specified in extra(select). 136 >>> for d in Article.objects.extra(select={'id_plus_one': 'id + 1'}).values('id', 'id_plus_one'): 138 137 ... i = d.items() 139 138 ... i.sort() … … 146 145 [('id', 7), ('id_plus_one', 8)] 147 146 [('id', 1), ('id_plus_one', 2)] 148 149 # however, an exception FieldDoesNotExist will still be thrown 150 # if you try to access non-existent field (field that is neither on the model nor extra) 151 >>> Article.objects.extra( select={'id_plus_one' : 'id + 1'} ).values('id', 'id_plus_two') 147 >>> data = {'id_plus_one': 'id+1', 'id_plus_two': 'id+2', 'id_plus_three': 'id+3', 148 ... 'id_plus_four': 'id+4', 'id_plus_five': 'id+5', 'id_plus_six': 'id+6', 149 ... 'id_plus_seven': 'id+7', 'id_plus_eight': 'id+8'} 150 >>> result = list(Article.objects.filter(id=1).extra(select=data).values(*data.keys()))[0] 151 >>> result = result.items() 152 >>> result.sort() 153 >>> result 154 [('id_plus_eight', 9), ('id_plus_five', 6), ('id_plus_four', 5), ('id_plus_one', 2), ('id_plus_seven', 8), ('id_plus_six', 7), ('id_plus_three', 4), ('id_plus_two', 3)] 155 156 # However, an exception FieldDoesNotExist will be thrown if you specify a 157 # non-existent field name in values() (a field that is neither in the model 158 # nor in extra(select)). 159 >>> Article.objects.extra(select={'id_plus_one': 'id + 1'}).values('id', 'id_plus_two') 152 160 Traceback (most recent call last): 153 161 ... 154 162 FieldDoesNotExist: Article has no field named 'id_plus_two' 155 163 156 # if you don't specify which fields, all are returned164 # If you don't specify field names to values(), all are returned. 157 165 >>> list(Article.objects.filter(id=5).values()) == [{'id': 5, 'headline': 'Article 5', 'pub_date': datetime(2005, 8, 1, 9, 0)}] 158 166 True
