【发布时间】:2021-03-04 02:19:46
【问题描述】:
有没有办法或包在 python3 manage.py shell 中以表格形式打印 Django 查询集
查询集打印如下
但我希望它像这样打印
【问题讨论】:
标签: django django-models django-shell
有没有办法或包在 python3 manage.py shell 中以表格形式打印 Django 查询集
查询集打印如下
但我希望它像这样打印
【问题讨论】:
标签: django django-models django-shell
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))
【讨论】: