django-ninja-ulid 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (43) hide show
  1. django_ninja_ulid-0.1.0/.github/workflows/ci.yml +101 -0
  2. django_ninja_ulid-0.1.0/.github/workflows/prepare-release.yml +77 -0
  3. django_ninja_ulid-0.1.0/.github/workflows/publish.yml +91 -0
  4. django_ninja_ulid-0.1.0/.gitignore +10 -0
  5. django_ninja_ulid-0.1.0/CHANGELOG.md +23 -0
  6. django_ninja_ulid-0.1.0/CONTRIBUTING.md +41 -0
  7. django_ninja_ulid-0.1.0/LICENSE +21 -0
  8. django_ninja_ulid-0.1.0/PKG-INFO +218 -0
  9. django_ninja_ulid-0.1.0/README.md +191 -0
  10. django_ninja_ulid-0.1.0/ci-matrix.json +7 -0
  11. django_ninja_ulid-0.1.0/docs/assets/publishing-setup.mmd +6 -0
  12. django_ninja_ulid-0.1.0/docs/assets/release-flow.mmd +6 -0
  13. django_ninja_ulid-0.1.0/docs/compatibility.md +70 -0
  14. django_ninja_ulid-0.1.0/docs/publishing-setup.md +116 -0
  15. django_ninja_ulid-0.1.0/docs/releasing.md +64 -0
  16. django_ninja_ulid-0.1.0/docs/verification-2026-10-18.json +534 -0
  17. django_ninja_ulid-0.1.0/docs/versioning.md +32 -0
  18. django_ninja_ulid-0.1.0/pyproject.toml +78 -0
  19. django_ninja_ulid-0.1.0/scripts/check_install.py +212 -0
  20. django_ninja_ulid-0.1.0/scripts/check_release.py +147 -0
  21. django_ninja_ulid-0.1.0/scripts/check_type_install.py +65 -0
  22. django_ninja_ulid-0.1.0/scripts/ci_matrix.py +34 -0
  23. django_ninja_ulid-0.1.0/scripts/run_matrix.py +103 -0
  24. django_ninja_ulid-0.1.0/src/django_ninja_ulid/__init__.py +5 -0
  25. django_ninja_ulid-0.1.0/src/django_ninja_ulid/_types.py +37 -0
  26. django_ninja_ulid-0.1.0/src/django_ninja_ulid/orm.py +100 -0
  27. django_ninja_ulid-0.1.0/src/django_ninja_ulid/py.typed +0 -0
  28. django_ninja_ulid-0.1.0/tests/alias_cases.py +77 -0
  29. django_ninja_ulid-0.1.0/tests/conftest.py +24 -0
  30. django_ninja_ulid-0.1.0/tests/poc/__init__.py +0 -0
  31. django_ninja_ulid-0.1.0/tests/poc/adapter.py +6 -0
  32. django_ninja_ulid-0.1.0/tests/poc/api.py +93 -0
  33. django_ninja_ulid-0.1.0/tests/poc/models.py +23 -0
  34. django_ninja_ulid-0.1.0/tests/poc/schemas.py +82 -0
  35. django_ninja_ulid-0.1.0/tests/poc/settings.py +26 -0
  36. django_ninja_ulid-0.1.0/tests/poc/urls.py +5 -0
  37. django_ninja_ulid-0.1.0/tests/test_database.py +21 -0
  38. django_ninja_ulid-0.1.0/tests/test_integration.py +184 -0
  39. django_ninja_ulid-0.1.0/tests/test_optional_integration.py +94 -0
  40. django_ninja_ulid-0.1.0/tests/test_registration.py +31 -0
  41. django_ninja_ulid-0.1.0/tests/test_release_checks.py +105 -0
  42. django_ninja_ulid-0.1.0/tests/test_response_schema.py +268 -0
  43. django_ninja_ulid-0.1.0/uv.lock +878 -0
@@ -0,0 +1,101 @@
1
+ name: CI
2
+
3
+ on:
4
+ push:
5
+ pull_request:
6
+ workflow_dispatch:
7
+ workflow_call:
8
+
9
+ permissions:
10
+ contents: read
11
+
12
+ concurrency:
13
+ group: ci-${{ github.workflow }}-${{ github.ref }}
14
+ cancel-in-progress: true
15
+
16
+ jobs:
17
+ matrix:
18
+ runs-on: ubuntu-latest
19
+ outputs:
20
+ cases: ${{ steps.cases.outputs.matrix }}
21
+ steps:
22
+ - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
23
+ with:
24
+ persist-credentials: false
25
+ - id: cases
26
+ run: python3 scripts/ci_matrix.py --github-output
27
+
28
+ test:
29
+ needs: matrix
30
+ name: Python ${{ matrix.python }} / Django ${{ matrix.django }} / ${{ matrix.database }}
31
+ runs-on: ubuntu-latest
32
+ timeout-minutes: 15
33
+ strategy:
34
+ fail-fast: false
35
+ max-parallel: 4
36
+ matrix: ${{ fromJSON(needs.matrix.outputs.cases) }}
37
+ services:
38
+ postgres:
39
+ image: ${{ matrix.postgres_image }}
40
+ env:
41
+ POSTGRES_USER: postgres
42
+ POSTGRES_PASSWORD: postgres
43
+ POSTGRES_DB: ulid_test
44
+ ports:
45
+ - 5432:5432
46
+ options: >-
47
+ --health-cmd pg_isready
48
+ --health-interval 10s
49
+ --health-timeout 5s
50
+ --health-retries 5
51
+ env:
52
+ ULID_TEST_DATABASE: ${{ matrix.database }}
53
+ ULID_POSTGRES_HOST: 127.0.0.1
54
+ ULID_POSTGRES_PORT: "5432"
55
+ ULID_POSTGRES_DB: ulid_test
56
+ ULID_POSTGRES_USER: postgres
57
+ ULID_POSTGRES_PASSWORD: postgres
58
+ DJANGO_VERSION: ${{ matrix.django }}
59
+ steps:
60
+ - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
61
+ with:
62
+ persist-credentials: false
63
+ - uses: actions/setup-python@5fda3b95a4ea91299a34e894583c3862153e4b97 # v7.0.0
64
+ with:
65
+ python-version: ${{ matrix.python }}
66
+ - uses: astral-sh/setup-uv@c771a70e6277c0a99b617c7a806ffedaca235ff9 # v9.0.0
67
+ with:
68
+ version: "0.7.8"
69
+ enable-cache: true
70
+ cache-dependency-glob: uv.lock
71
+ - name: Install locked build and test tools
72
+ run: uv sync --locked --extra orm --python python
73
+ - name: Build wheel from sdist and check metadata
74
+ run: |
75
+ uv build --offline
76
+ .venv/bin/python -m twine check dist/*
77
+ - name: Install the wheel and selected Django
78
+ run: |
79
+ uv pip install --python .venv/bin/python --reinstall-package django-ninja-ulid dist/*.whl "Django==$DJANGO_VERSION"
80
+ uv pip check --python .venv/bin/python
81
+ - name: Test the installed wheel
82
+ run: .venv/bin/python -m pytest -q --junitxml=local-checks/pytest.xml
83
+ - name: Check migrations and API in a new project
84
+ run: .venv/bin/python scripts/check_install.py --output local-checks/fresh-project.json
85
+ - name: Check base installation without the ORM extra
86
+ if: matrix.database == 'sqlite'
87
+ run: |
88
+ uv venv --python python local-checks/base-venv
89
+ uv pip install --python local-checks/base-venv/bin/python dist/*.whl "Django==$DJANGO_VERSION"
90
+ uv pip check --python local-checks/base-venv/bin/python
91
+ local-checks/base-venv/bin/python -I scripts/check_type_install.py --output local-checks/base-install.json
92
+ - uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
93
+ if: always()
94
+ with:
95
+ name: checks-py${{ matrix.python }}-django${{ matrix.django }}-${{ matrix.database }}
96
+ path: |
97
+ local-checks/pytest.xml
98
+ local-checks/fresh-project.json
99
+ local-checks/base-install.json
100
+ if-no-files-found: warn
101
+ retention-days: 7
@@ -0,0 +1,77 @@
1
+ name: Prepare release
2
+
3
+ on:
4
+ workflow_dispatch:
5
+ inputs:
6
+ release_tag:
7
+ description: "照合する予定タグ。タグは作成しません(例: v0.1.0)"
8
+ required: false
9
+ default: ""
10
+ type: string
11
+ workflow_call:
12
+ inputs:
13
+ release_tag:
14
+ required: false
15
+ default: ""
16
+ type: string
17
+ outputs:
18
+ version:
19
+ value: ${{ jobs.build.outputs.version }}
20
+ artifact_name:
21
+ value: ${{ jobs.build.outputs.artifact_name }}
22
+
23
+ permissions:
24
+ contents: read
25
+
26
+ concurrency:
27
+ group: prepare-${{ github.workflow }}-${{ github.ref }}
28
+ cancel-in-progress: true
29
+
30
+ jobs:
31
+ build:
32
+ runs-on: ubuntu-latest
33
+ timeout-minutes: 10
34
+ outputs:
35
+ version: ${{ steps.inspect.outputs.version }}
36
+ artifact_name: ${{ steps.inspect.outputs.artifact_name }}
37
+ env:
38
+ RELEASE_TAG: ${{ inputs.release_tag }}
39
+ steps:
40
+ - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
41
+ with:
42
+ persist-credentials: false
43
+ - uses: actions/setup-python@5fda3b95a4ea91299a34e894583c3862153e4b97 # v7.0.0
44
+ with:
45
+ python-version: "3.13"
46
+ - uses: astral-sh/setup-uv@c771a70e6277c0a99b617c7a806ffedaca235ff9 # v9.0.0
47
+ with:
48
+ version: "0.7.8"
49
+ enable-cache: true
50
+ cache-dependency-glob: uv.lock
51
+ - name: Install build tools
52
+ run: uv sync --locked --extra orm --python python
53
+ - name: Build and inspect the release files
54
+ id: inspect
55
+ run: |
56
+ uv build --offline --out-dir local-checks/release/dist
57
+ .venv/bin/python -m twine check local-checks/release/dist/*
58
+ .venv/bin/python scripts/check_release.py --dist-dir local-checks/release/dist --manifest local-checks/release/release-manifest.json --tag "$RELEASE_TAG" --commit "$GITHUB_SHA" --github-output
59
+ - name: Check the ORM wheel in a new project
60
+ run: |
61
+ uv pip install --python .venv/bin/python --reinstall-package django-ninja-ulid local-checks/release/dist/*.whl
62
+ uv pip check --python .venv/bin/python
63
+ .venv/bin/python scripts/check_install.py --output local-checks/release/orm-install.json
64
+ - name: Check the wheel without the ORM extra
65
+ run: |
66
+ uv venv --python python local-checks/release-base
67
+ uv pip install --python local-checks/release-base/bin/python local-checks/release/dist/*.whl
68
+ uv pip check --python local-checks/release-base/bin/python
69
+ local-checks/release-base/bin/python -I scripts/check_type_install.py --output local-checks/release/type-install.json
70
+ - name: Verify file hashes after installation checks
71
+ run: python scripts/check_release.py --verify --dist-dir local-checks/release/dist --manifest local-checks/release/release-manifest.json
72
+ - uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
73
+ with:
74
+ name: ${{ steps.inspect.outputs.artifact_name }}
75
+ path: local-checks/release/
76
+ if-no-files-found: error
77
+ retention-days: 30
@@ -0,0 +1,91 @@
1
+ # Tag releases publish to TestPyPI and verify the downloaded wheel.
2
+ # Production PyPI publishing remains disabled pending a separate release decision.
3
+ name: Publish
4
+
5
+ on:
6
+ push:
7
+ tags:
8
+ - "v*"
9
+
10
+ permissions:
11
+ contents: read
12
+
13
+ concurrency:
14
+ group: publish-${{ github.ref }}
15
+ cancel-in-progress: false
16
+
17
+ jobs:
18
+ tests:
19
+ uses: ./.github/workflows/ci.yml
20
+ build:
21
+ needs: tests
22
+ uses: ./.github/workflows/prepare-release.yml
23
+ with:
24
+ release_tag: ${{ github.ref_name }}
25
+
26
+ testpypi:
27
+ needs: build
28
+ runs-on: ubuntu-latest
29
+ environment:
30
+ name: testpypi
31
+ url: https://test.pypi.org/project/django-ninja-ulid/
32
+ permissions:
33
+ id-token: write
34
+ steps:
35
+ - uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2
36
+ with:
37
+ name: ${{ needs.build.outputs.artifact_name }}
38
+ path: bundle/
39
+ - uses: pypa/gh-action-pypi-publish@dc37677b2e1c63e2034f94d8a5b11f265b73ba33 # v1.14.2
40
+ with:
41
+ repository-url: https://test.pypi.org/legacy/
42
+ packages-dir: bundle/dist/
43
+
44
+ check-testpypi:
45
+ needs: [build, testpypi]
46
+ runs-on: ubuntu-latest
47
+ env:
48
+ RELEASE_VERSION: ${{ needs.build.outputs.version }}
49
+ steps:
50
+ - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
51
+ with:
52
+ persist-credentials: false
53
+ - uses: actions/setup-python@5fda3b95a4ea91299a34e894583c3862153e4b97 # v7.0.0
54
+ with:
55
+ python-version: "3.13"
56
+ - uses: astral-sh/setup-uv@c771a70e6277c0a99b617c7a806ffedaca235ff9 # v9.0.0
57
+ with:
58
+ version: "0.7.8"
59
+ - uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2
60
+ with:
61
+ name: ${{ needs.build.outputs.artifact_name }}
62
+ path: bundle/
63
+ - name: Verify the build artifact
64
+ run: python scripts/check_release.py --verify --dist-dir bundle/dist --manifest bundle/release-manifest.json
65
+ - name: Download only this package from TestPyPI
66
+ run: |
67
+ python -m pip download --no-cache-dir --no-deps --only-binary=:all: --index-url https://test.pypi.org/simple/ --dest downloaded "django-ninja-ulid==$RELEASE_VERSION"
68
+ python scripts/check_release.py --downloaded-wheel downloaded/*.whl --manifest bundle/release-manifest.json
69
+ - name: Check the downloaded package with locked PyPI dependencies
70
+ run: |
71
+ uv sync --locked --extra orm --python python
72
+ uv pip install --python .venv/bin/python --no-deps --reinstall-package django-ninja-ulid downloaded/*.whl
73
+ .venv/bin/python scripts/check_install.py
74
+
75
+ pypi:
76
+ if: ${{ false }} # Enable only with a protected pypi environment and owner approval.
77
+ needs: [build, check-testpypi]
78
+ runs-on: ubuntu-latest
79
+ environment:
80
+ name: pypi
81
+ url: https://pypi.org/project/django-ninja-ulid/
82
+ permissions:
83
+ id-token: write
84
+ steps:
85
+ - uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2
86
+ with:
87
+ name: ${{ needs.build.outputs.artifact_name }}
88
+ path: bundle/
89
+ - uses: pypa/gh-action-pypi-publish@dc37677b2e1c63e2034f94d8a5b11f265b73ba33 # v1.14.2
90
+ with:
91
+ packages-dir: bundle/dist/
@@ -0,0 +1,10 @@
1
+ .venv/
2
+ __pycache__/
3
+ *.py[cod]
4
+ .pytest_cache/
5
+ .mypy_cache/
6
+ .ruff_cache/
7
+ /dist/
8
+ /local-checks/
9
+ *.egg-info/
10
+ .DS_Store
@@ -0,0 +1,23 @@
1
+ # Changelog
2
+
3
+ ## 0.1.0 (公開準備中)
4
+
5
+ 初回の通常リリースとして準備している版です。公開日とタグは、実際の公開時に記録します。
6
+
7
+ ### 追加する機能
8
+
9
+ - `ApiULID`による26文字ULIDの検証、正規化、文字列化。
10
+ - `create_ulid_response_schema`による、通常の主キーを参照する平坦な応答Schemaの生成。
11
+ - 手書きSchemaで使う`related_ids`。
12
+ - `python-ulid-django`を`orm` extraへ分離。
13
+ - Python 3.12〜3.14、Django 5.2.18/6.1.2、SQLite/PostgreSQL 18.6の12通りを対応範囲とする。
14
+ - wheelの通常インストールとextraなしの型利用を確認するGitHub Actionsのマトリクス。
15
+ - 配布版・予定タグ・メタデータ・必要な同梱ファイル・SHA256を確認する、アップロードを伴わないリリース準備。
16
+
17
+ ### 初回の対象外と制限
18
+
19
+ - 応答Schemaの生成は平坦な主キー参照のみ。入力・PATCHの自動生成、ネスト、非主キーの`to_field`などは対象外。
20
+ - `by_alias`設定が異なる応答には、別名のSchemaが必要。
21
+ - OpenAPIの応答パターンは小文字も許容するが、実出力は大文字。
22
+
23
+ 破壊的変更は`0.x`のマイナー版、互換性を保つ修正はパッチ版とする。[互換性方針](docs/versioning.md)
@@ -0,0 +1,41 @@
1
+ # 開発と不具合報告
2
+
3
+ 初回版の範囲は、26文字ULIDの型と、既存ULIDFieldから平坦な応答Schemaを作る処理です。機能の追加や対応範囲を変える場合は、[README](README.md)と[互換性方針](docs/versioning.md)を基に検討します。
4
+
5
+ ## 開発環境
6
+
7
+ ```bash
8
+ uv sync --locked --extra orm
9
+ uv run --locked --extra orm pytest -q
10
+ ```
11
+
12
+ 配布物を作って検査する場合は、次を実行します。
13
+
14
+ ```bash
15
+ uv build
16
+ uv run --locked --extra orm twine check dist/*
17
+ ```
18
+
19
+ `tests/poc`には、このライブラリを作る前のPoCのAPIを、テスト用に残しています。`tests/poc/adapter.py`はインストールした`django_ninja_ulid`を読み込むので、元のPoCの実装には依存しません。wheelに含まれるのは`src/django_ninja_ulid`だけです。
20
+
21
+ Python・Django・DBの全組み合わせを確認する手順は[対応環境](docs/compatibility.md)にあります。DBの実験にはテスト専用のサーバーを使います。配布物は[リリース手順](docs/releasing.md)で作成し、ソースからの利用と通常インストールの両方を確認します。
22
+
23
+ ## 不具合報告に添える情報
24
+
25
+ 不具合は、[このリポジトリのIssues](https://github.com/nibuno/django-ninja-ulid/issues)へ次の情報を添えて報告してください。
26
+
27
+ - Python、django-ninja-ulid、Django、Ninja、Pydantic、ULIDライブラリ、DBの版。
28
+ - 問題を再現する最小のモデル・Schema・API定義。
29
+ - 再現する入力、期待した応答、実際の応答と例外。
30
+ - `by_alias`、関連の定義、`fields`、prefetchの有無。
31
+
32
+ 実データ、認証情報、接続先の秘密情報は含めず、再現用の値へ置き換えます。
33
+
34
+ ## 変更時の確認
35
+
36
+ - 公開APIの変更には、受け入れる入力・出力と変更理由を示す。
37
+ - 変更の影響を確認できるテストを追加し、対応するマトリクスを通す。
38
+ - 利用方法や互換性が変わる場合はREADMEとCHANGELOGを更新する。
39
+ - 対応環境を広げる場合は、依存指定とCIを同時に更新する。
40
+
41
+ 入力・PATCHの自動生成、ネスト、逆参照、非主キーの`to_field`などは現在の対象外です。実装に先立ち、用途と受け入れ条件を相談する範囲とします。
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 django-ninja-ulid contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,218 @@
1
+ Metadata-Version: 2.5
2
+ Name: django-ninja-ulid
3
+ Version: 0.1.0
4
+ Summary: ULID validation and flat model response schemas for Django Ninja.
5
+ Project-URL: Homepage, https://github.com/nibuno/django-ninja-ulid
6
+ Project-URL: Source, https://github.com/nibuno/django-ninja-ulid
7
+ Project-URL: Issues, https://github.com/nibuno/django-ninja-ulid/issues
8
+ Project-URL: Changelog, https://github.com/nibuno/django-ninja-ulid/blob/main/CHANGELOG.md
9
+ License-Expression: MIT
10
+ License-File: LICENSE
11
+ Keywords: api,django,django-ninja,pydantic,ulid
12
+ Classifier: Development Status :: 3 - Alpha
13
+ Classifier: Framework :: Django
14
+ Classifier: Programming Language :: Python :: 3
15
+ Classifier: Programming Language :: Python :: 3.12
16
+ Classifier: Programming Language :: Python :: 3.13
17
+ Classifier: Programming Language :: Python :: 3.14
18
+ Classifier: Typing :: Typed
19
+ Requires-Python: <3.15,>=3.12
20
+ Requires-Dist: django!=6.0.*,!=6.1.0,!=6.1.1,<6.2,>=5.2.18
21
+ Requires-Dist: django-ninja==1.7.1
22
+ Requires-Dist: pydantic==2.13.5
23
+ Requires-Dist: python-ulid[pydantic]==4.0.1
24
+ Provides-Extra: orm
25
+ Requires-Dist: python-ulid-django==1.6.0; extra == 'orm'
26
+ Description-Content-Type: text/markdown
27
+
28
+ # django-ninja-ulid
29
+
30
+ [![CI](https://github.com/nibuno/django-ninja-ulid/actions/workflows/ci.yml/badge.svg)](https://github.com/nibuno/django-ninja-ulid/actions/workflows/ci.yml)
31
+ [![License: MIT](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/nibuno/django-ninja-ulid/blob/main/LICENSE)
32
+
33
+ [Django Ninja](https://django-ninja.dev/)のAPIで、[ULID](https://github.com/ulid/spec)を26文字の文字列として入出力するためのライブラリです。
34
+
35
+ - **`ApiULID`**:ULIDを検証・正規化し、26文字の文字列として出力するPydanticの型
36
+ - **`create_ulid_response_schema`**:ULIDFieldを含むDjangoモデルから、応答用のSchemaを作る関数
37
+
38
+ IDの保存と生成は、既存の[python-ulid-django](https://pypi.org/project/python-ulid-django/)に任せます。このライブラリは、API側の型と変換だけを担当します。
39
+
40
+ ## なぜ必要か
41
+
42
+ python-ulid-djangoの`ULIDField`をDjango Ninjaの`ModelSchema`でそのまま使うと、次の問題が起きます。
43
+
44
+ - `ULIDField`は`UUIDField`を継承しているため、NinjaはこのフィールドをUUID型として扱います。26文字のULIDを入力すると、UUIDとして検証されて422になります。
45
+ - 応答では、ULIDオブジェクトがJSONに変換できず、500になります。
46
+ - `register_field("UUIDField", ...)`で型を差し替えると、通常の`UUIDField`までULIDとして扱われてしまいます。
47
+
48
+ このライブラリは、ULIDのフィールドだけを判別して型を差し替えます。通常のUUIDはUUIDのまま残ります。
49
+
50
+ ## インストール
51
+
52
+ ```bash
53
+ # 型(ApiULID)だけを使う場合
54
+ pip install django-ninja-ulid
55
+
56
+ # Djangoモデルとの連携も使う場合
57
+ pip install "django-ninja-ulid[orm]"
58
+ ```
59
+
60
+ `orm` extraを付けると、python-ulid-djangoも一緒にインストールされます。
61
+
62
+ ## 使い方
63
+
64
+ ### `ApiULID`:入力と出力の型
65
+
66
+ ```python
67
+ from ninja import NinjaAPI, Schema
68
+ from django_ninja_ulid import ApiULID
69
+
70
+ api = NinjaAPI()
71
+
72
+
73
+ class ItemIn(Schema):
74
+ owner_id: ApiULID
75
+
76
+
77
+ @api.get("/items/{item_id}")
78
+ def get_item(request, item_id: ApiULID):
79
+ ...
80
+ ```
81
+
82
+ - 入力は26文字のASCII文字列だけを受け付けます。小文字は大文字に正規化します。
83
+ - 使えない文字、128bitを超える値(先頭が8以降)、整数・真偽値・null・オブジェクト・配列は、422の検証エラーになります。
84
+ - 出力は、常に大文字26文字の文字列です。
85
+ - nullを許可したい場合は、`ApiULID | None`と書きます。
86
+ - `ApiULID`だけを使う場合は、Djangoの設定もpython-ulid-djangoも読み込みません。Pydanticの`TypeAdapter`からも使えます。
87
+
88
+ ### `create_ulid_response_schema`:モデルから応答Schemaを作る
89
+
90
+ ```python
91
+ from django.db import models
92
+ from ulid import ULID
93
+ from ulid_django.models import ULIDField
94
+
95
+
96
+ class Owner(models.Model):
97
+ id = ULIDField(primary_key=True, default=ULID, editable=False)
98
+
99
+
100
+ class Tag(models.Model):
101
+ id = ULIDField(primary_key=True, default=ULID, editable=False)
102
+
103
+
104
+ class Item(models.Model):
105
+ id = ULIDField(primary_key=True, default=ULID, editable=False)
106
+ public_uuid = models.UUIDField()
107
+ name = models.CharField(max_length=100)
108
+ owner = models.ForeignKey(Owner, null=True, on_delete=models.SET_NULL)
109
+ tags = models.ManyToManyField(Tag)
110
+ ```
111
+
112
+ ```python
113
+ from django.shortcuts import get_object_or_404
114
+ from django_ninja_ulid.orm import create_ulid_response_schema
115
+
116
+ ItemOut = create_ulid_response_schema(
117
+ Item,
118
+ name="ItemOut",
119
+ fields=["id", "public_uuid", "name", "owner", "tags"],
120
+ )
121
+
122
+
123
+ @api.get("/items/{item_id}", response=ItemOut)
124
+ def get_item(request, item_id: ApiULID):
125
+ return get_object_or_404(Item.objects.prefetch_related("tags"), pk=item_id)
126
+ ```
127
+
128
+ ```json
129
+ {
130
+ "id": "01JABCDXYZ0123456789ABCDEF",
131
+ "public_uuid": "8f14e45f-ceea-467f-a0e6-4b6f8b8a8c1d",
132
+ "name": "example",
133
+ "owner": "01JABCDXYZ0123456789ABCDEG",
134
+ "tags": ["01JABCDXYZ0123456789ABCDEH"]
135
+ }
136
+ ```
137
+
138
+ - ULIDのフィールドだけを`ApiULID`に差し替えます。ULID以外のフィールドは、Ninjaの型推論のまま残ります。
139
+ - 外部キーと多対多は、関連先のモデルではなく主キーのULIDを返します。
140
+ - 公開するフィールドは`fields`で必ず指定します。空のリストは拒否します。
141
+ - `django_ninja_ulid.orm`は、Djangoの初期化が終わってからimportしてください。
142
+
143
+ | モデルの定義 | 応答での扱い |
144
+ |---|---|
145
+ | ULIDField(`null=False`) | 必須、nullにならない |
146
+ | ULIDField・外部キー(`null=True`) | 必須、nullになりうる |
147
+ | 多対多 | 必須、空の配列はありうる、nullにならない |
148
+ | ULID以外のフィールド | Ninjaの通常の扱いのまま |
149
+
150
+ ### 手書きの`ModelSchema`で使う
151
+
152
+ Schemaを自分で書く場合は、`ApiULID`と`related_ids`を組み合わせます。
153
+
154
+ ```python
155
+ from typing import Annotated
156
+
157
+ from ninja import ModelSchema
158
+ from pydantic import BeforeValidator, Field
159
+
160
+ from django_ninja_ulid import ApiULID
161
+ from django_ninja_ulid.orm import related_ids
162
+
163
+
164
+ class ItemOut(ModelSchema):
165
+ id: ApiULID
166
+ owner: ApiULID | None = Field(..., alias="owner_id")
167
+ tags: Annotated[list[ApiULID], BeforeValidator(related_ids)]
168
+
169
+ class Meta:
170
+ model = Item
171
+ fields = ["id", "public_uuid", "name", "owner", "tags"]
172
+ ```
173
+
174
+ `related_ids`は、Manager・QuerySet・モデルのリストを主キーのリストに変換します。
175
+
176
+ ## 対応範囲
177
+
178
+ `create_ulid_response_schema`は、ネストしない応答専用です。次の設定を渡すと`ValueError`になります。
179
+
180
+ - 空の`fields`
181
+ - 主キー以外を参照する`to_field`付きの外部キー
182
+ - 逆参照、`OneToOneField`、複合主キー
183
+
184
+ 入力用・PATCH用Schemaの自動生成、ネストした応答、python-ulid-django以外のULIDFieldには、まだ対応していません。
185
+
186
+ ## 既知の制限
187
+
188
+ - Ninja 1.7.1では、同じSchemaを`by_alias`の設定が違うルートで使い回すと、OpenAPIの定義が後から登録したほうで上書きされます。HTTPの応答自体は正しく返ります。出力形式ごとに、別の`name`でSchemaを作ってください。
189
+ - OpenAPIでは入力と応答に同じ文字パターンを使っているため、応答側も小文字を許すように書かれます。実際の出力は常に大文字です。
190
+
191
+ ## 動作確認済みの環境
192
+
193
+ | Python | Django | DB |
194
+ |---|---|---|
195
+ | 3.12 / 3.13 / 3.14 | 5.2.18 / 6.1.2 | SQLite / PostgreSQL 18.6 |
196
+
197
+ 上の12通りすべての組み合わせを、GitHub Actionsで検証しています。依存ライブラリは、検証済みの次の版に固定しています。
198
+
199
+ - Django Ninja 1.7.1
200
+ - Pydantic 2.13.5
201
+ - python-ulid 4.0.1
202
+ - python-ulid-django 1.6.0(`orm` extra)
203
+
204
+ 詳しい検証内容は[docs/compatibility.md](https://github.com/nibuno/django-ninja-ulid/blob/main/docs/compatibility.md)にまとめています。
205
+
206
+ ## バージョンの方針
207
+
208
+ 公開APIは、`ApiULID`、`create_ulid_response_schema`、`related_ids`の3つです。
209
+
210
+ 0.x系の間は、互換性のない変更をマイナーバージョン(例:0.1 → 0.2)で、互換性を保つ修正をパッチバージョン(例:0.1.0 → 0.1.1)で出します。変更内容と移行方法は[CHANGELOG](https://github.com/nibuno/django-ninja-ulid/blob/main/CHANGELOG.md)に記録します。詳しくは[docs/versioning.md](https://github.com/nibuno/django-ninja-ulid/blob/main/docs/versioning.md)を見てください。
211
+
212
+ ## 開発に参加する
213
+
214
+ 不具合の報告や開発の手順は、[CONTRIBUTING.md](https://github.com/nibuno/django-ninja-ulid/blob/main/CONTRIBUTING.md)にまとめています。
215
+
216
+ ## ライセンス
217
+
218
+ [MIT License](https://github.com/nibuno/django-ninja-ulid/blob/main/LICENSE)