Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 31 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ def listOfPersons(request):

```

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:

- Generate HTML table from querset:

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

order_queryset = Order.objects.all().values("customer", "product", "amount")
reportTitle = "Order List"
columnsToBeSummarized = ['amount']
fontName = "Arial"
cssClasses = "reportTable container"
headerRowBackgroundColor = '#ffeeee'
evenRowsBackgroundColor = '#ffeeff'
oddRowsBackgroundColor = '#ffffff'
rowIndexVisibility = True
table = DjangoQtt.generate_from_queryset(reportTitle, order_queryset, columnsToBeSummarized, cssClasses,
"ltr", fontName, "Total amount", rowIndexVisibility,
headerRowBackgroundColor, evenRowsBackgroundColor, oddRowsBackgroundColor
)
order_queryset = Order.objects.annotate(
**{
'Order__Number': F('order_number'),
'Order__Item': F('order_item'),
'Customer__Name': F('customer_name'),
'Order__Date': F('order_date'),
'Total__Amount': F('total_amount'),
}
).values(
'Order__Number',
'Order__Item',
'Customer__Name',
'Order__Date',
'Total__Amount'
)

table = DjangoQtt.generate_from_queryset(
title = "Summmary Table",
queryset = order_queryset,
htmlClass = "summary",
rowIndex = True,
footerCols=['Total__Amount'],

)

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

```
```

The table will be look like this:

![table](docs/django_query_to_table.jpg)
4 changes: 3 additions & 1 deletion django_query_to_table/DjangoQtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,13 @@ def _generate_report(cursor, title, sqltext, footerCols, htmlClass, direction, f
sumOfColumn = None
if footerCols:
sumOfColumn = _calculate_sums(data, columns, footerCols, totalText)

columns_for_display = list(map(lambda s: s.replace("__", " "), columns))

context = {
'title': title,
'data': data,
'columns': columns,
'columns': columns_for_display,
'sumOfColumn': sumOfColumn,
'direction': direction,
'font': font,
Expand Down
Binary file added docs/django_query_to_table.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.