1

How to pass dynamic type column names to values_list() and values() method in django?

  • Possible duplicate of [Split by comma and strip whitespace in Python](http://stackoverflow.com/questions/4071396/split-by-comma-and-strip-whitespace-in-python) – JRodDynamite May 10 '16 at 12:43

1 Answers1

4

To send a column names dynamically to values_list():

You can either send as a list or tuple or dictionary as a keyword arguments. But the problem is values_list() method does not accept arguments as list or tuple or dictionary.

So the solution is, have those dynamic column names in either list or tuple and receive that as a keyword argument.

i.e, list = ['id', 'first_name', 'gender'] if this is your list of column names needs to pass to values_list().

querySet = User.objects.filter(username = 'mahajan535').values_list(*list)

If you have dictionary to store dynamic column names then querySet = User.objects.filter(username = 'mahajan535').values_list(**dict)

This solves you problem.

For more info: https://docs.python.org/2/tutorial/controlflow.html#unpacking-argument-lists