[Django]-How to get the list of columns in a Django Queryset?

5๐Ÿ‘

โœ…

I got it!

print('qset1:', len(qset1), qset1[0].__dict__.keys())
print('qset2:', len(qset2), qset2[0].__dict__.keys())
print('qset3:', len(qset2), qset3[0].__dict__.keys())
...

qset[0] gives the first row of the queryset qset (This only work if the queryset has at least one row)

.__dict__ converts that row to a dictionary โ€“ a dictionary with keys being column names and values are the row elements

.keys() gives the list of keys of the dictionary, thus, the columns

This prints the list of the columns for all the querysets.

๐Ÿ‘คGranny Aching

Leave a comment