Skip to main content

Django app that lets you define custom SQL functions

Project description

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 and Use

  1. Install using your favorite python package manager, eg. pip install django-sqlfun.
  2. Add sqlfun to INSTALLED_APPS in your django settings.
  3. Define a custom function in a module that gets imported on project load (eg. models.py). See below for example, or the test_project.
  4. Run manage.py makemigrations and then 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 and --name options of makemigrations are respected

Credits

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

Project details


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.0.4.tar.gz (15.6 kB view details)

Uploaded Source

Built Distribution

django_sqlfun-0.0.4-py3-none-any.whl (9.7 kB view details)

Uploaded Python 3

File details

Details for the file django-sqlfun-0.0.4.tar.gz.

File metadata

  • Download URL: django-sqlfun-0.0.4.tar.gz
  • Upload date:
  • Size: 15.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: pdm/2.4.3 CPython/3.9.16

File hashes

Hashes for django-sqlfun-0.0.4.tar.gz
Algorithm Hash digest
SHA256 8435cf75fe48a300420e1294523024e5c77d34e516f15a6eb5fb298ce0d9e898
MD5 ee8b0f5005bdbbc0b72efc4dc11b5741
BLAKE2b-256 17514d2602c6ea093e65d9d8c679106730ac5887d2c8f3b39de8532095383305

See more details on using hashes here.

File details

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

File metadata

  • Download URL: django_sqlfun-0.0.4-py3-none-any.whl
  • Upload date:
  • Size: 9.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: pdm/2.4.3 CPython/3.9.16

File hashes

Hashes for django_sqlfun-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 175c44c771cdedde1765f0112dd38a78be5444938939f00510e00e6401b7eec7
MD5 af79b0054fa9ef2df4c2b2d80ff6ebbb
BLAKE2b-256 a98099660d4814520b1f8d1060f47c728ea89f40d256680609394245ffa3f25a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page