Skip to content

Commit 16396ba

Browse files
committed
Add tabulate_qs and print_qs
1 parent f3ae4fc commit 16396ba

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

requirements/base.txt

+1
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,4 @@ google-auth-oauthlib==1.2.1
3131

3232
pyotp==2.9.0
3333
qrcode==8.0
34+
tabulate==0.9.0
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from tabulate import tabulate
2+
3+
4+
def tabulate_qs(queryset, *, fields: list[str] | None = None, exclude: list[str] | None = None) -> str:
5+
# Make sure the table won't be empty
6+
if not fields:
7+
fields = [field.name for field in queryset.model._meta.fields]
8+
9+
if not exclude:
10+
exclude = []
11+
12+
fields = [field for field in fields if field not in exclude]
13+
14+
return tabulate(
15+
tabular_data=queryset.values_list(*fields),
16+
headers=fields,
17+
tablefmt="github",
18+
)
19+
20+
21+
def print_qs(queryset, *, fields: list[str] | None = None, exclude: list[str] | None = None) -> None:
22+
print(tabulate_qs(queryset, fields=fields, exclude=exclude))

0 commit comments

Comments
 (0)