Skip to content

Commit 1e3d564

Browse files
committed
doc: add example for generate_from_queryset
1 parent 7df2921 commit 1e3d564

File tree

1 file changed

+37
-7
lines changed

1 file changed

+37
-7
lines changed

README.md

+37-7
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,14 @@
44

55
You can read more about this package here : [django query to table package](https://mshaeri.com/blog/generate-html-table-report-from-sql-query-in-django/)
66

7-
The package contains one function named "generateFromSql" accepting 12 arguments :
7+
The package contains two functions named:
8+
- **generate_from_sql**: Generate HTML table by given SQL query
9+
- **generate_from_queryset**:Generate HTML table by given Django queryset
10+
11+
Parameters:
812

9-
* cursor : DB cursor
1013
* title : The title of the report that will be shown on top of table
11-
* sqltext : The sql select query to retrieve data
14+
* sqltext/queryset : The sql select query to retrieve data / django queryset
1215
* footerCols : A list of columns name that you want to have Sum of values on footer . Example : ['amount','price']
1316
* htmlClass : Html CSS classes for the table
1417
* direction (default = "ltr") : Indicates direction of the report page. "ltr"- Left to Right , "rtl" - Right to Left
@@ -28,11 +31,11 @@ Run following command to install DjangoQtt :
2831
pip install django-query-to-table
2932
```
3033

31-
## Usage :
34+
## Example usage :
35+
36+
- Generate HTML table by SQL query:
3237

33-
- Generate html table by SQL query
3438
```python
35-
from django.db import connection
3639
from django_query_to_table import DjangoQtt
3740
from django.http import HttpResponse
3841

@@ -52,8 +55,35 @@ def listOfPersons(request):
5255
headerRowBackgroundColor, evenRowsBackgroundColor, oddRowsBackgroundColor
5356
)
5457

55-
# here the table is a string variable contianing the html table showing the query result
58+
# here the table is a string variable containing the html table showing the query result
5659
return HttpResponse(table)
5760

5861
```
5962

63+
- Generate HTML table from querset:
64+
65+
```python
66+
from django_query_to_table import DjangoQtt
67+
from django.http import HttpResponse
68+
from .models import Order
69+
# view function in Django project
70+
def listOfPersons(request):
71+
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+
)
85+
86+
# here the table is a string variable containing the html table showing the queryset result
87+
return HttpResponse(table)
88+
89+
```

0 commit comments

Comments
 (0)