2

有没有办法或包在 python3 manage.py shell 中以表格形式打印 Django 查询集

查询集打印如下

这个

但我希望它像这样打印

这个

4

1 回答 1

2

Django 没有这个功能,但是使用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))
于 2020-11-20T17:15:57.853 回答