Django REST Framework backend generator: schema to REST API

Auxen, by Auxen Prime, generates a Django REST Framework (Python) backend from your database schema or a plain-English description. The output is an ordinary Django REST Framework project that you download and own. Install the requirements and run it with manage.py or Gunicorn.

What you get

The table is read from a real generated project (83 files for the example schema). The last column is the file in that project.

AreaWhat is generatedWhere in the project
REST APICreate, read, update and delete endpoints for each entity, under /api/<entity>example_app/views/customer_view.py
AuthenticationRegistration and login with JWT in this example. The builder also offers OAuth2, Session, API Key, Basic Auth and OpenID Connect.example_app/views/auth_views.py
RBACRole and permission tables, and a permission check on the protected routesexample_app/permissions/rbac_permission.py
MigrationsAn initial migration for the schemasql/V1__initial_schema.sql
Tests3 test files in the example (unit and integration)tests/test_customer.py
DockerfileBuilds the image and runs the app as a non-root user (uid 10001)Dockerfile
docker-composeRuns the app with a PostgreSQL database beside itdocker-compose.yml
CI pipelineA GitHub Actions workflow, when you choose it.github/workflows/ci.yml
API descriptionAn OpenAPI description of the APIexample_app/urls.py
Health checkA health endpointexample_app/urls.py
ConfigurationAn .env.example listing the settings.env.example

The example it was generated from

Entities Customer and Invoice, with Invoice many-to-one Customer, on PostgreSQL, JWT with RBAC. Options: unit tests, integration tests, Docker, GitHub Actions pipeline and migrations. Auxen adds the authentication tables (users, roles, permissions, sessions and others) around them.

Folder layout, with file counts
.github/workflows/  1 file
docs/  2 files
example_app/  63 files
example_app/authentication/  2 files
example_app/management/  3 files
example_app/middleware/  3 files
example_app/models/  8 files
example_app/permissions/  3 files
example_app/queries/  14 files
example_app/serializers/  8 files
example_app/services/  5 files
example_app/utils/  2 files
example_app/views/  9 files
sql/  1 file
tests/  6 files
(project root)  10 files

Real excerpts

A generated controller example_app/views/customer_view.py
class CustomerViewSet(viewsets.ModelViewSet):
    """
    ViewSet for Customer CRUD operations.

    Permissions required (when auth is enabled):
    - CUSTOMER_READ: list, retrieve
    - CUSTOMER_CREATE: create
    - CUSTOMER_UPDATE: update, partial_update
    - CUSTOMER_DELETE: destroy
    """
    queryset = Customer.objects.all()
    serializer_class = CustomerSerializer
    permission_classes = [HasEntityPermission]
    # Entity name for permission checking
The RBAC check example_app/permissions/rbac_permission.py
class HasPermission(BasePermission):
    """
    Custom permission class for RBAC.
    Usage: permission_classes = [IsAuthenticated, HasPermission]
    Set required_permissions attribute on the view.
    """

    def has_permission(self, request, view):
        # Get required permissions from view
        required_permissions = getattr(view, 'required_permissions', None)

        if not required_permissions:
The first migration, for Customer sql/V1__initial_schema.sql
CREATE TABLE IF NOT EXISTS "customer" (
    customer_id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    customer_name VARCHAR(255) NOT NULL,
    customer_email VARCHAR(255) UNIQUE NOT NULL,
    customer_birthday DATE,
    customer_active BOOLEAN NOT NULL,
    customer_gender VARCHAR(255) NOT NULL,
    customer_notes TEXT,
    customer_created_at TIMESTAMP NOT NULL
);
The Dockerfile Dockerfile
FROM python:3.11-slim
# The root filesystem is read-only under the Helm chart: write no __pycache__.
ENV PYTHONDONTWRITEBYTECODE=1
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
# The app's migrations are made from its models here, when the image is built
# (the root filesystem is read-only at run time). A project that keeps its own
# migrations gets only what its models changed since. The build reads no real
# database: SQLite in memory, and a throwaway key settings.py insists on.
RUN SECRET_KEY=build-only DB_ENGINE=django.db.backends.sqlite3 DB_NAME=:memory: \
    python manage.py makemigrations example_app --noinput
# Run as an unprivileged user (uid and gid 10001) that owns /app.
RUN groupadd --system --gid 10001 app && useradd --system --no-create-home --gid app --uid 10001 app && chown -R app:app /app
USER 10001:10001
A generated test tests/test_customer.py
class TestCustomer:
    def test_create(self, api):
        row = api.create(Customer)
        assert row["customer_id"]

    def test_list(self, api):
        row = api.create(Customer)
        res = api.get(f"{PATH}/")
        assert res.status_code == 200, res.content
        assert row["customer_id"] in [item["customer_id"] for item in res.json()["results"]]

    def test_get_by_id(self, api):

What it is built on

Read from the generated manifest: Django (requirements.txt), Django REST framework (requirements.txt), SimpleJWT (requirements.txt), drf-spectacular (OpenAPI) (requirements.txt), pytest (requirements-dev.txt).

Databases

Django REST Framework generates for PostgreSQL, MySQL, MariaDB, SQLite, SQL Server, Oracle and Amazon Redshift. It does not generate for MongoDB.

How the generator is tested

These checks live in the Auxen repository and run against the generators themselves. They are listed here as what they do, not as a guarantee about every schema you might write.

  • Generation test (npm run test:gen): generates this stack for several example schemas, then checks the syntax of the result with Python.
  • Runtime test (npm run test:runtime): starts the generated application, connects it to a real database, registers a user, logs in, then creates and lists records over HTTP.
  • Security scan: the generated project, including its Dockerfile and CI files, is scanned with Trivy for vulnerabilities, secrets and misconfiguration. The check fails on any secret, and on any high or critical finding that has a fix.
  • Docker build test (npm run test:docker, run on demand): builds each stack's own Dockerfile.

Running and deploying it

Install the requirements and run it with manage.py or Gunicorn. You download the project and deploy it wherever you run software. Auxen does not deploy the generated backend for you yet. It has a hosted sandbox where you can run the code and try its API before you download it.

Review the code before it carries real traffic, as you would any code you did not write. The terms of service say the same: generated code is provided as is, and you are responsible for reviewing, testing and securing it.

More

Auxen is made by Auxen Prime Technologies Limited and lives at auxenprime.com. It is not affiliated with other companies that use the name Auxen.