MY_FIELDS = ['id', 'created', 'И_еще_28']
qs = Sheets.objects.all()
v = []
v += [list(qs.values(v).distinct()) for v in MY_FIELDS]
qs = Sheets.objects.all().values(*MY_FIELDS).distinct()
result = None
for field in MY_FIELDS:
query = Sheets.objects.all().annotate(
field_name=Value(field),
field_value=F(field),
).values(
'field_name',
'field_value'
).distinct()
if result is None:
result = query
else:
result = result.union(query, all=True)
print(result.values())
Your second approach does produce unique values, but these are unique combinations. So if there are 10 fields with each 5 values, that already can result in at most 510 results, so it scales dramatically.
But the good news is, we can solve this, by aggregating all columns into distinct lists. Indeed, with ArrayAgg [Django-doc]:
MY_FIELDS = ['id', 'created', 'And_more_28'] from django.contrib.postgres.aggregates import ArrayAgg qs = Sheets.objects.aggregate( **{'values_{f}': ArrayAgg('f', distinct=True) for f in MY_FIELDS} ) v = [qs[f'values_{f}'] for f in MY_FIELDS]
This will produce lists of distinct values for each column, and all in one single query.