{"id":37679778,"url":"https://github.com/radusuciu/django-sqlfun","last_synced_at":"2026-01-16T12:22:45.656Z","repository":{"id":79558572,"uuid":"597937550","full_name":"radusuciu/django-sqlfun","owner":"radusuciu","description":"A Django app that lets you define custom SQL functions","archived":false,"fork":false,"pushed_at":"2024-03-01T19:18:01.000Z","size":152,"stargazers_count":1,"open_issues_count":6,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-11-06T16:21:50.906Z","etag":null,"topics":["django","sql"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/radusuciu.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2023-02-06T03:06:45.000Z","updated_at":"2025-07-26T02:56:23.000Z","dependencies_parsed_at":"2024-02-28T00:38:12.826Z","dependency_job_id":null,"html_url":"https://github.com/radusuciu/django-sqlfun","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"purl":"pkg:github/radusuciu/django-sqlfun","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/radusuciu%2Fdjango-sqlfun","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/radusuciu%2Fdjango-sqlfun/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/radusuciu%2Fdjango-sqlfun/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/radusuciu%2Fdjango-sqlfun/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/radusuciu","download_url":"https://codeload.github.com/radusuciu/django-sqlfun/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/radusuciu%2Fdjango-sqlfun/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28478582,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-16T11:59:17.896Z","status":"ssl_error","status_checked_at":"2026-01-16T11:55:55.838Z","response_time":107,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["django","sql"],"created_at":"2026-01-16T12:22:45.562Z","updated_at":"2026-01-16T12:22:45.634Z","avatar_url":"https://github.com/radusuciu.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![PyPI pyversions](https://img.shields.io/pypi/pyversions/django-sqlfun.svg)](https://pypi.python.org/pypi/django-sqlfun/)\n[![Django versions](https://img.shields.io/pypi/frameworkversions/django/django-sqlfun)](https://pypi.python.org/pypi/django-sqlfun/)\n[![PyPI version](https://img.shields.io/pypi/v/django-sqlfun.svg)](https://pypi.python.org/pypi/django-sqlfun/)\n[![GitHub release](https://img.shields.io/github/release/radusuciu/django-sqlfun.svg)](https://github.com/radusuciu/django-sqlfun/releases/)\n\n# Django SQL Fun\n\nDjango 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`](https://docs.djangoproject.com/en/5.0/ref/models/expressions/#func-expressions).\n\n**Note**: I'm still developing this so there may be some rough edges. Breaking changes may happen.\n\n## Installation\n\n1. Install using your favorite python package manager, eg. `pip install django-sqlfun`.\n2. Add `sqlfun` to `INSTALLED_APPS` in your django settings\n3. Run `manage.py migrate`. This will set up any tables required by `sqlfun` to keep track of your custom funcitons\n\n## Use\n\n1. Define a custom function in a module that gets imported on project load (eg. `models.py`). See below for example, or the [`test_project`](tests/test_project).\n2. Run `manage.py makemigrations`\n3. Run `manage.py migrate`\n\n### Example\n\nDefine a custom function in your `models.py`:\n\n```python\n# models.py\nfrom sqlfun import SqlFun\nfrom django.db.models import IntegerField\n\nclass BadSum(SqlFun):\n    \"\"\"Almost returns the sum of two numbers.\"\"\"\n    \n    app_label = 'test_project' # [optional] if omitted, sqlfun will atempt to auto-resolve it\n    sql = \"\"\"\n        CREATE OR REPLACE FUNCTION bad_sum(\n            first integer,\n            second integer\n        ) RETURNS integer as $$\n        SELECT first + second + 1;\n        $$\n        LANGUAGE sql\n        stable;\n    \"\"\"\n    output_field = IntegerField()\n```\n\nThen 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)))`.\n\n### Notes\n\n- SQL functions are normalized, so changes in white-space should not result in changes being detected\n- the `--dry-run` and `--name` options of `makemigrations` are respected\n\n## Development\n\nThese instructions assume a recent Ubuntu/Debian environment.\n\n1. Clone the repository\n2. If needed, install `python3-venv` and `python3-pip` packages\n3. Create a virtual environment `python3 -m venv .venv`\n4. Install `libpq-dev` package since `psycopg2` depends on it.\n5. Install `pdm`: `pip3 install --user pdm`\n6. Install dev dependencies with `pdm install --dev`\n\nTesting also requires a recent install of docker which is used to spin up a test postgres instance.\n\n## Credits\n\nThis project is inspired by two great projects: [`django-pgtrigger`](https://github.com/Opus10/django-pgtrigger) and [`django-pgviews`](https://github.com/mypebble/django-pgviews).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fradusuciu%2Fdjango-sqlfun","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fradusuciu%2Fdjango-sqlfun","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fradusuciu%2Fdjango-sqlfun/lists"}