Skip to content

About

Opinionated Django styleguide for async-first, service-layer architecture with Pydantic, django-ninja, and pgtrigger

Topics

Resources

Stars

13 stars

Watchers

0 watching

Forks

Latest commit

 

History

11 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Django Styleguide

An opinionated styleguide for service-layer Django: business logic in services and selectors, Pydantic schemas at the service input, django-modern-rest for HTTP APIs and PostgreSQL triggers for the invariants the database must hold.

There are two guides. Each is complete, so read the one that fits your project:

Your project Read Background work
Async: ASGI, async def services and controllers ASYNC.md TaskIQ
Sync: WSGI, plain def services and controllers SYNC.md Celery

The two guides give the same architecture and the same rules for models, validation, triggers, errors and large tables. They differ where the code differs: how a service opens a transaction, how a controller is written, how tests run, and which task queue carries background work.

The async guide describes the form we run. The sync guide is the same guide in sync form.

What both guides assume

How to read a rule

  • A rule stated as must or never is one we defend. The guide gives its reason and says what breaks without it.
  • A rule stated as we prefer is house taste. A team can decide differently and nothing breaks, as long as the whole project decides the same way.

Helper code

core/ holds the helpers the guides name. Copy it into your project's own core app; the guides import it as project.core.

Module What it gives
core/exceptions.py ApplicationError and the two errors the helpers raise
core/services.py model_update, validated_get_or_create, validated_update_or_create, each with an a-prefixed async form
core/db/models.py TimeStampedModel, VersionedModel, VersionField
core/db/pgtrigger/ pgtrigger_ignore, and the AutoModified and Versioning triggers
core/db/keyset.py keyset_iterator and akeyset_iterator for large streams
core/db/batched_delete.py batched_delete and abatched_delete for bounded deletes

Tests for the helper code

testproject/ is a small Django project that runs the helpers against a real PostgreSQL, in their sync and async forms. It needs uv and a PostgreSQL server:

docker run -d --rm --name styleguide-pg -e POSTGRES_PASSWORD=postgres \
    -p 127.0.0.1:5544:5432 postgres:18

cd testproject
uv run pytest

docker stop styleguide-pg

POSTGRES_HOST and POSTGRES_PORT point the tests at another server. The tests need the psycopg 3 driver; the psycopg 2 path of pgtrigger_ignore has no test.

Credits

The service and selector layers come from HackSoft's Django Styleguide. This guide started from it and has since gone its own way, so the two differ in most details.

About

Opinionated Django styleguide for async-first, service-layer architecture with Pydantic, django-ninja, and pgtrigger

Topics

Resources

Stars

13 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages