1

Is there a way or package to print Django queryset in Tabular form in python3 manage.py shell

The queryset prints like

this

But I want it to be printed like

this

Siraj Alam
  • 9,217
  • 9
  • 53
  • 65
Aaqib
  • 367
  • 3
  • 14

1 Answers1

2

Django doesn't have this feature, but it would be easy to create a function using tabulate

from tabulate import tabulate

def output_table(queryset, limit=50):
    headers = [x.name for x in queryset.model._meta.fields]
    rows = queryset.values_list(*headers)
    if limit is not None:
        rows = rows[:limit]
    print(tabulate(rows, headers))
Aaqib
  • 367
  • 3
  • 14
Alasdair
  • 298,606
  • 55
  • 578
  • 516
  • Thanks. It's doing wonders but isn't there any package which does this out of box without writing custom code? – Aaqib Nov 20 '20 at 18:10
  • There might be packages that do something like this but I don’t know of any. – Alasdair Nov 20 '20 at 22:11