1

I want to pass list elements as a separated string to value mehtod. This is my main query:

resumes = JobRecord.objects.all().values('field1','field2','field3','field4')

Instead of writing the each field of my JobRecord model in the values method, I want to send this list values method:

fields_LIST =['field1', 'field2', 'field3', 'field4']

How can I pass elements of list as a separated string to method ?

Hints

I tested these solutions, but none of them responded:

Vay 1:

str(fields_LIST) # result: "['field1', 'field2', 'field3', 'field4']"

Vay 2:

str1 = ''
for item in fields_LIST:
    str1 += '\'' + item + '\'' + ',' # result: "'field1','field2','field3','field4'," 

Vay 3:

pass list itself:

JobRecord.objects.all().values(fields_LIST) # result: Error :'list' object has no attribute 'split'

Vay 4:

str1 = ','.join(str(e) for e in fields_LIST) # result: 'field1, field2, field3, field4'

Studies I did study this resource and questions: 1,2,3,4,5,6

Tom Carrick
  • 6,349
  • 13
  • 54
  • 78
rahnama7m
  • 865
  • 10
  • 38

1 Answers1

6

Just use *:

JobRecord.objects.all().values(*fields_LIST)
Sergey Pugach
  • 5,561
  • 1
  • 16
  • 31