An open API service indexing awesome lists of open source software.

https://github.com/roskakori/django-reporting-with-raw-sql

Code and slides for talk at PyCon Austria 2025
https://github.com/roskakori/django-reporting-with-raw-sql

django example reporting sql

Last synced: about 2 months ago
JSON representation

Code and slides for talk at PyCon Austria 2025

Awesome Lists containing this project

README

          

# Django reporting with raw SQL

This is a Python Django example application to show how to perform Django reporting by means of raw SQL and database views.

## Environment settings

The project requires an environment file named `.env` to know about the [PostgreSQL](https://www.postgresql.org/) database settings and the preferred default password for demo data.

For example:

```dotenv
# The default password for demo data.
MT_DEFAULT_PASSWORD="deMo.123"

# PotgreSQL database connection
MT_POSTGRES_HOST=localhost
MT_POSTGRES_PORT=5422
MT_POSTGRES_DATABASE=minitrack_local
MT_POSTGRES_USERNAME=minitrack_local
MT_POSTGRES_PASSWORD=${MT_DEFAULT_PASSWORD}
```

### PostgreSQL from docker container

By default, the application will connect to a local server running on port 5432 to a database `minitrack_local` with a user `minitrack_local` and the password `deMo.123`. The repository includes a `compose.yaml` which can be used with [Docker](https://www.docker.com/):

```bash
docker compose up
```

### Existing PostgreSQL server

If you already have access to an existing Postgre server, specify the settings in the `.env`.

## Project setup

The project requires [uv](https://docs.astral.sh/uv/).

To set up the project, run:

```bash
uv sync --group dev
```

In order to keep code clean even after your own modifications, activate the pre-commit hooks:

```bash
uv run pre-commit install --install-hooks
```

Next, apply the database migrations:

```bash
uv run python manage.py migrate
```

After that, create an admin user to log in. The fastest way for that is:

```bash
uv run python manage.py make_demo_admin
```

This adds an admin user named "admin" with the password specified with the environment variable `MT_DEFAULT_PASSWORD` (default: "deMo.123").

Alternatively, you can use the standard management command `createsuperuser` and specify all the details yourself:

```bash
uv run python manage.py createsuperuser --username admin
```

Then run the server

```bash
uv run python manage.py runserver
```

Now connect to and use the login from the `createsuperuser` from above.

You can use the admin UI to create and edit data.

To get you started quickly, create a few random demo data by running:

```bash
uv run python manage.py make_demo
```

## License

Copyright (c) 2025 Thomas Aglassinger. Distributed under the [MIT License](LICENSE).