[Django]-Django: values_list(flat=True) but for multiple fields

4👍

You can use itertools chain approach. It will flatten your list.

import itertools

qs = TestModel.objects.values_list('FieldA','FieldB')
qs = list(itertools.chain(*qs))
return qs

1👍

You can use this approach:

fields = ['FieldA','FieldB']
testQS = list(TestModel.objects.values_list(*fields))
result = {field:[] for field in fields}
counter = 0
for item in testQS:
    result = {field:[*result[field],item[counter]] for field in fields}
    counter +=1

1👍

You can use TestModel.objects.values_list(‘FieldA’,’FieldB’, named=True)

Leave a comment