[Fixed]-Pass list of fields to QuerySet.values( )

40👍

You can use the * operator to expand a list out into separate arguments when passed to a function, as described here in the Python tutorial.

>>> qs = User.objects.all()
>>> values = ['first_name', 'email']
>>> qs.values(*values)

yields

[{'first_name': u'aaaa', 'email': u'a@b.com'}, 
 {'first_name': u'', 'email': u'abc@def.com'}, 
 {'first_name': u'', 'email': u'abcd@gmail.com'},
 '...(remaining elements truncated)...']

(I further truncated the output for brevity).

Leave a comment