有没有办法或包在 python3 manage.py shell 中以表格形式打印 Django 查询集
查询集打印如下
但我希望它像这样打印
有没有办法或包在 python3 manage.py shell 中以表格形式打印 Django 查询集
查询集打印如下
但我希望它像这样打印
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))