Django app that lets you define custom SQL functions
Project description
django-sqlfun
Django SQLFun allows you to define custom SQL functions in code which are then kept up to date in the database with every run of manage.py migrate
.
Use
- Install using your favorite python package manager, eg.
pip install django-sqlfun
- Add
django_sqlfun
toINSTALLED_APPS
in your django settings. - Define a custom function in a module that gets imported on project load (eg.
models.py
). See below for example. - Run
manage.py migrate
Example
from django_sqlfun import SqlFun
class BadSum(SqlFun):
""Almost returns the sum of two numbers."""
sql = """
CREATE OR REPLACE FUNCTION bad_sum(
first integer,
second integer
) RETURNS integer as $$
SELECT first + second + 1;
$$
LANGUAGE sql
stable;
"""
Planned Developments
This is alpha level software, and thus far only tested with Postgres.
- Add support for usage in Python code by inheriting from
django.db.models.expressions.Func
. - Integrate into migrations system. Currently if you use functions that are defined through
django-sqlfun
in django migrations, you may experience errors since the latest version of that function will always be used. I believe this can be tackled with the same approach used by thedjango-pgtrigger
project. - Handle deleted custom functions. Currently if you delete a
django-sqlfun
function, it is not automatically deleted in the database itself. This could likely also be solved by integrating into the migrations system.
Ideas for improvement are welcome. Breaking changes are possible.
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.1.tar.gz
(3.3 kB
view hashes)
Built Distribution
Close
Hashes for django_sqlfun-0.0.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8d0946981dda805472dbc76bfe6a653fb8605d68417ea89b3c5d24a9e0d0df51 |
|
MD5 | 345fc4ec2409d38b247ed4a06a6aacfc |
|
BLAKE2b-256 | dcc4e2f8d1b9848d132d792131fb2a2950e95fa14f38063a2879014df2c4db38 |