Skip to main content

PyPI pyversions Django versions PyPI version GitHub release

Django SQL Fun

Django SQLFun allows you to define and manage custom SQL functions in code. When you change the function definitions and call makemigrations, it will generate migrations for any functions that have been added, removed, or changed. These function classes can also be used in Django querysets since the SqlFun class inherits from django.db.models.expressions.Func.

Note: I'm still developing this so there may be some rough edges. Breaking changes may happen.

Installation

  1. Install using your favorite python package manager, eg. pip install django-sqlfun.
  2. Add sqlfun to INSTALLED_APPS in your django settings
  3. Run manage.py migrate. This will set up any tables required by sqlfun to keep track of your custom funcitons

Use

  1. Define a custom function in a module that gets imported on project load (eg. models.py). See below for example, or the test_project.
  2. Run manage.py makemigrations
  3. Run manage.py migrate

Example

Define a custom function in your models.py:

# models.py
from sqlfun import SqlFun
from django.db.models import IntegerField

class BadSum(SqlFun):
    """Almost returns the sum of two numbers."""
    
    app_label = 'test_project' # [optional] if omitted, sqlfun will atempt to auto-resolve it
    sql = """
        CREATE OR REPLACE FUNCTION bad_sum(
            first integer,
            second integer
        ) RETURNS integer as $$
        SELECT first + second + 1;
        $$
        LANGUAGE sql
        stable;
    """
    output_field = IntegerField()

Then run manage.py makemigrations and manage.py migrate and you should be good to go. You can use it in SQL: SELECT bad_sum(2, 2), or in a Python queryset like so: MyModel.objects.annotate(foo=BadSum(Value(2), Value(2))).

Notes

  • SQL functions are normalized, so changes in white-space should not result in changes being detected
  • the --dry-run, --name, and --check options of makemigrations are respected. --check exits with a non-zero status if any sqlfun function changes are missing migrations (in addition to Django's own model-change check), writes nothing, and requires a reachable, migrated database — it fails rather than silently passing if sqlfun changes cannot be evaluated.

Development

These instructions assume a recent Ubuntu/Debian environment.

  1. Clone the repository
  2. Install uv
  3. Install the libpq-dev package since psycopg2 depends on it.
  4. Install dependencies with uv sync (this creates .venv and installs the dev group)

Testing also requires a recent install of docker which is used to spin up a test postgres instance.

Credits

This project is inspired by two great projects: django-pgtrigger and django-pgviews.

Download files

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

Source Distribution

django_sqlfun-0.1.0.tar.gz (8.2 kB view details)

Uploaded Source

Built Distribution

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

django_sqlfun-0.1.0-py3-none-any.whl (9.8 kB view details)

Uploaded Python 3

File details

Details for the file django_sqlfun-0.1.0.tar.gz.

File metadata

  • Download URL: django_sqlfun-0.1.0.tar.gz
  • Upload date:
  • Size: 8.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.28 {"installer":{"name":"uv","version":"0.11.28","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for django_sqlfun-0.1.0.tar.gz
Algorithm Hash digest
SHA256 d35755ba0f6d2d5c9f54b1033c5b4a8d0bfbbd290579d94197b2a97808a26d95
MD5 fd7f5ca2791431566780e6c95fcf7ee4
BLAKE2b-256 282f354ff32348a163cca3f8eb94e38574657243f737f52bf72fbf1261da5cb1

See more details on using hashes here.

File details

Details for the file django_sqlfun-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: django_sqlfun-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 9.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.28 {"installer":{"name":"uv","version":"0.11.28","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for django_sqlfun-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3d3d62f07a8dc0a0fb2f9f8c916ae5442005f057ae4892af7459698bf5244126
MD5 5ae270661cbfae4ba806d5da1a7f3cda
BLAKE2b-256 79b2d3afab0339e125b5a19a92085685f9c147393de9a8aa9ac3494a545728da

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 files

0.0.7

2 files

0.0.6

2 files

0.0.5

2 files

0.0.4

2 files

0.0.2

2 files

0.0.1

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page