This repo is a sample for:
a) writing different types of SQL queries using Django ORM, following the examples in https://davit.tech/django-queryset-examples
b) running unit-tests in GitHub Actions including PostgreSQL
git clone git@github.com:jabadia/django-querysets.git
cd django-querysets
pipenv sync
3. Decide if you want to run tests against a local postgres server or launch a docker container postgres server
Export variables pointing to your local server (tests will run in a newly created database named test_queries
)
export POSTGRES_PORT=5432
export POSTGRES_USER=<user>
export POSTGRES_PASSWORD=<pass>
# in another terminal
cd db
docker-compose up
export POSTGRES_PORT=5556
export POSTGRES_USER=postgres
export POSTGRES_PASSWORD=secretpass
DJANGO_SETTINGS_MODULE=querysets.test_settings pipenv run python manage.py test
# or
export DJANGO_SETTINGS_MODULE=querysets.test_settings
pipenv run python manage.py test
# create the production database
PGPASSWORD=secretpass createdb -h localhost -p 5556 queries -U postgres --no-password
# launch the server
export DJANGO_SETTINGS_MODULE=querysets.test_settings
pipenv run python manage.py migrate
pipenv run python manage.py runserver 7777
open your browser pointing to http://localhost:7777
In settings.py
look for the LOGGING
key and enable django.db
DEBUG level
LOGGING = {
...
'loggers': {
...
'django.db': {
'level': 'DEBUG', # log SQL queries
},
},
}
In any place of the code where you want to log queries, enable it by writing:
getLogger('django.db').setLevel(logging.DEBUG)
Using a query capture context manager in one particular place of the code:
from django.test.utils import CaptureQueriesContext
from django.db import connection
...
def one_function():
...
with CaptureQueriesContext(connection( as queries:
< call function or execute code that launches queries here >
...
for query in queries.captured_queries:
print(query)
In docker-compose.yml
uncomment the logging option:
- "postgres"
# - "-c" # uncomment this
# - "log_statement=all" # uncomment this