Django app that lets you define custom SQL functions
Project description
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
- Install using your favorite python package manager, eg.
pip install django-sqlfun. - Add
sqlfuntoINSTALLED_APPSin your django settings - Run
manage.py migrate. This will set up any tables required bysqlfunto keep track of your custom funcitons
Use
- Define a custom function in a module that gets imported on project load (eg.
models.py). See below for example, or thetest_project. - Run
manage.py makemigrations - 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-runand--nameoptions ofmakemigrationsare respected
Development
These instructions assume a recent Ubuntu/Debian environment.
- Clone the repository
- If needed, install
python3-venvandpython3-pippackages - Create a virtual environment
python3 -m venv .venv - Install
libpq-devpackage sincepsycopg2depends on it. - Install
pdm:pip3 install --user pdm - Install dev dependencies with
pdm install --dev
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.
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file django-sqlfun-0.0.7.tar.gz.
File metadata
- Download URL: django-sqlfun-0.0.7.tar.gz
- Upload date:
- Size: 11.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: pdm/2.12.4 CPython/3.10.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3aca4262c1def71a49403560ea9ae4585c8c49e1cde1b184eda8e5afda62695b
|
|
| MD5 |
e6c5cb6941ef1a135270f54015460ce5
|
|
| BLAKE2b-256 |
3347147301cff849775919562a38f18b3392a450e863e9dcaf923451452982a0
|
File details
Details for the file django_sqlfun-0.0.7-py3-none-any.whl.
File metadata
- Download URL: django_sqlfun-0.0.7-py3-none-any.whl
- Upload date:
- Size: 8.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: pdm/2.12.4 CPython/3.10.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c774703d1e50a11e5a960becf7b90f0b49d79358c928069d46672a7afe84b120
|
|
| MD5 |
a0d4662f8a06e95b3a93ab6876fafdc6
|
|
| BLAKE2b-256 |
18138641b39eb3e4e45c97887aec25085d7e65803f8dfc43c2439ec7360befc5
|