Django- How do I select particular columns from a model?

Viewed 42

I have an array of columns. Like,

fields = ['first_name', 'last_name', 'email']
# this values can be dynamic

I want the values of those columns from the User model.

I tried this.

User.objects.values(fields)

But it doesn't work. It provides a traceback.

AttributeError: 'list' object has no attribute 'split'

Any solution?

1 Answers

Here is how you do it:

fields = ['first_name', 'last_name', 'email']

User.objects.values(*fields)
Related