Is it possible to use a list of fields to query a model in Django

Is it possible to use a list of fields to query a model in Django

Problem Description:

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.

Something like:

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)
Rate this post
We use cookies in order to give you the best possible experience on our website. By continuing to use this site, you agree to our use of cookies.
Accept
Reject