Skip to content

Commit 5a99643

Browse files
authored
Merge pull request #3 from birddevelper/fix/resolve-white-space-issue-in-django-queryset
fix: resolve white space issue in django queryset #2
2 parents 96e9102 + be0a48e commit 5a99643

File tree

3 files changed

+34
-16
lines changed

3 files changed

+34
-16
lines changed

README.md

+31-15
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ def listOfPersons(request):
6060

6161
```
6262

63+
Since Django 4.0.4 introduced a security fix that disallows spaces in aliases, you can use double underscores (__) as a substitute in your aliases. The table generator will automatically display them as spaces in the output. Here's an example:
64+
6365
- Generate HTML table from querset:
6466

6567
```python
@@ -69,21 +71,35 @@ from .models import Order
6971
# view function in Django project
7072
def listOfPersons(request):
7173

72-
order_queryset = Order.objects.all().values("customer", "product", "amount")
73-
reportTitle = "Order List"
74-
columnsToBeSummarized = ['amount']
75-
fontName = "Arial"
76-
cssClasses = "reportTable container"
77-
headerRowBackgroundColor = '#ffeeee'
78-
evenRowsBackgroundColor = '#ffeeff'
79-
oddRowsBackgroundColor = '#ffffff'
80-
rowIndexVisibility = True
81-
table = DjangoQtt.generate_from_queryset(reportTitle, order_queryset, columnsToBeSummarized, cssClasses,
82-
"ltr", fontName, "Total amount", rowIndexVisibility,
83-
headerRowBackgroundColor, evenRowsBackgroundColor, oddRowsBackgroundColor
84-
)
74+
order_queryset = Order.objects.annotate(
75+
**{
76+
'Order__Number': F('order_number'),
77+
'Order__Item': F('order_item'),
78+
'Customer__Name': F('customer_name'),
79+
'Order__Date': F('order_date'),
80+
'Total__Amount': F('total_amount'),
81+
}
82+
).values(
83+
'Order__Number',
84+
'Order__Item',
85+
'Customer__Name',
86+
'Order__Date',
87+
'Total__Amount'
88+
)
89+
90+
table = DjangoQtt.generate_from_queryset(
91+
title = "Summmary Table",
92+
queryset = order_queryset,
93+
htmlClass = "summary",
94+
rowIndex = True,
95+
footerCols=['Total__Amount'],
96+
97+
)
8598

86-
# here the table is a string variable containing the html table showing the queryset result
8799
return HttpResponse(table)
88100

89-
```
101+
```
102+
103+
The table will be look like this:
104+
105+
![table](docs/django_query_to_table.jpg)

django_query_to_table/DjangoQtt.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,13 @@ def _generate_report(cursor, title, sqltext, footerCols, htmlClass, direction, f
6262
sumOfColumn = None
6363
if footerCols:
6464
sumOfColumn = _calculate_sums(data, columns, footerCols, totalText)
65+
66+
columns_for_display = list(map(lambda s: s.replace("__", " "), columns))
6567

6668
context = {
6769
'title': title,
6870
'data': data,
69-
'columns': columns,
71+
'columns': columns_for_display,
7072
'sumOfColumn': sumOfColumn,
7173
'direction': direction,
7274
'font': font,

docs/django_query_to_table.jpg

300 KB
Loading

0 commit comments

Comments
 (0)