Skip to main content

A simple to use Django package to turn your queryset and sql query into a beautiful reporting html table

Project description

Downloads

django-query-to-table (DjangoQtt) is an easy to use django package to generate html table from django queryset or sql query.

You can read more about this package here : django query to table package

The package has two functions, named:

  • generate_from_sql: Generate HTML table by given SQL query
  • generate_from_queryset:Generate HTML table by given Django queryset

Parameters and options:

  • title : The title of the report that will be shown on top of table
  • sqltext/queryset : The sql select query to retrieve data / django queryset
  • footerCols : A list of columns name that you want to have Sum of values on footer . Example : ['amount','price']
  • htmlClass : Html CSS classes for the table
  • direction (default = "ltr") : Indicates direction of the report page. "ltr"- Left to Right , "rtl" - Right to Left
  • font (default = "Tahoma") : Font of title and table contents
  • totalText (default = "Total") : Title of footer row that will be the put below the first column.
  • rowIndex (default = False) : Indicates whether the table should have index column or not.
  • headerRowColor (default = '#eeeeee') : The header (title) row background color.
  • evenRowColor (default = '#ffffff') : The even rows background color.
  • oddRowColor (default = '#ffffff') : The odd rows background color.

Installation

Run following command to install DjangoQtt :

pip install django-query-to-table

Example usage :

  • Generate HTML table by SQL query:
from django_query_to_table import DjangoQtt
from django.http import HttpResponse

# view function in Django project
def listOfPersons(request):
  reportTitle = "Employee List"
  sqlQuery = "SELECT FirstName as 'First Name', LastName as 'Last Name', phone as 'Phone Number', salary as 'Salary' FROM persons"
  columnsToBeSummarized = ['Salary']
  fontName = "Arial"
  cssClasses = "reportTable container"
  headerRowBackgroundColor = '#ffeeee'
  evenRowsBackgroundColor = '#ffeeff'
  oddRowsBackgroundColor = '#ffffff'
  rowIndexVisibility = True
  table = DjangoQtt.generate_from_sql(reportTitle, sqlQuery, columnsToBeSummarized, cssClasses,
                                  "ltr", fontName, "Total Salary", rowIndexVisibility,
                                  headerRowBackgroundColor, evenRowsBackgroundColor, oddRowsBackgroundColor
                                  )
  
  # here the table is a string variable containing the html table showing the query result
  return HttpResponse(table)
   
  • Generate HTML table from querset:

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:

from django_query_to_table import DjangoQtt
from django.http import HttpResponse
from .models import Order
# view function in Django project
def listOfPersons(request):

  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'],

                                    )
  
  return HttpResponse(table)
   

The table will be look like this:

table

If you find this package useful, please consider giving it a star! ⭐ It helps support the project and lets others discover it.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_query_to_table-1.1.1.tar.gz (4.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_query_to_table-1.1.1-py3-none-any.whl (5.3 kB view details)

Uploaded Python 3

File details

Details for the file django_query_to_table-1.1.1.tar.gz.

File metadata

  • Download URL: django_query_to_table-1.1.1.tar.gz
  • Upload date:
  • Size: 4.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for django_query_to_table-1.1.1.tar.gz
Algorithm Hash digest
SHA256 96a5480de1d024eb9b5a43ae38aa46120a46ca9315a234783fff0ec90c5cfaff
MD5 438ffcc31afcd212eb8df4b6b9936d22
BLAKE2b-256 9d6e8e923954705694cdfeca50e92004c4ed07b9486fc1d7110a0a54e6106c57

See more details on using hashes here.

File details

Details for the file django_query_to_table-1.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for django_query_to_table-1.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 36227e96da3afbd685a474521dc13f93f83cff49fbcf9bdef16c513ab31f8775
MD5 39a8c73458ff0fe3b7c431e24899ebcf
BLAKE2b-256 1856bc0c043aa735baf72c10ada35d40ed180dbef6ef61ce76911b6f5b9f40a1

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page