Md. Zayed Hassan
Md. Zayed Hassan

Reputation: 306

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

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?

Upvotes: 3

Views: 221

Answers (1)

token
token

Reputation: 933

Here is how you do it:

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

User.objects.values(*fields)

Upvotes: 2

Related Questions