Is it possible to use a list of fields to query a model in Django
At the outset, let me be upfront that I am not too comfortable with what I am trying to achieve.
But, here it is:
I am trying to query a random model in my app for which the field names would be passed at runtime.
fields_list = [field1, field2, ...] qs_target_model = target_model.values(fields_list) #, field3)
Using the above setup, I get an error:
‘list’ object has no attribute ‘split’
The reason I am trying to do this: Both the model and its fields would be selected at runtime!!
My question is:
Is what I am trying to do possible at all?
Solution – 1
Try this, fields_list must be the fields of model
fields_list = [field1, field2, ...] qs_target_model = target_model.values(*fields_list) #, field3)