I am new to python and django.I am having a list obtained dynamically containing database table fieldnames. How do I use this list within values_list() in django queryset while fetching results from database?
fieldList=['field1','field2'] #list containing table fields
obj=sampletable.objects.filter(somecondition).values_list(fieldlist) #--->want like this
I came to know that we can't use lists simply as it is inside values_list().So I converted it into a string like this and then tried it but in vain.
fieldListstr=','.join(repr(e) for e in fieldList)
This is the error which I got
Cannot resolve keyword "'field1','field2'" into field. Choices are: field1, field2
Please help me with your solutions. And thanks in advance
You can use argument list unpacking to pass the values of a list as arguments to a function like so:
values_list(*fieldlist)
Simply unpack them,
.values_list(*fieldlist)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With