Arun
Arun

Reputation: 27

Dynamic fields querying in django

I have a model containing 50 plus fields, while I would like to fetch the specific fields say some 3 or 4 fields only from database.

In case of static query, the above could be solved as follows

field_value = model.objects.values_list(field1, field3, field9)

while how the same could be replicated when the above fields are dynamically chosen.

Is there any way if we could store the fields for retrieval as a list and use the list as input parameter for values_list.

Upvotes: 0

Views: 37

Answers (1)

neverwalkaloner
neverwalkaloner

Reputation: 47364

You can unpack any list and pass it to the function this way:

fields_set = ['field1', 'field2', 'filed3']
field_value = model.objects.values_list(*fields_set)

Upvotes: 1

Related Questions