pydantic-djmodel 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.
- pydantic_djmodel-0.1.0/LICENSE +21 -0
- pydantic_djmodel-0.1.0/PKG-INFO +50 -0
- pydantic_djmodel-0.1.0/README.md +29 -0
- pydantic_djmodel-0.1.0/pyproject.toml +126 -0
- pydantic_djmodel-0.1.0/src/pydantic_djmodel/__init__.py +18 -0
- pydantic_djmodel-0.1.0/src/pydantic_djmodel/_settings.py +0 -0
- pydantic_djmodel-0.1.0/src/pydantic_djmodel/core.py +117 -0
- pydantic_djmodel-0.1.0/src/pydantic_djmodel/py.typed +0 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025, Ivan Ogasawara
|
|
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,50 @@
|
|
|
1
|
+
Metadata-Version: 2.3
|
|
2
|
+
Name: pydantic-djmodel
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: pydantic-djmodel is a Python library that converts Pydantic BaseModels into Django ORM models.
|
|
5
|
+
License: MIT
|
|
6
|
+
Author: Ivan Ogasawara
|
|
7
|
+
Author-email: ivan.ogasawara@gmail.com
|
|
8
|
+
Requires-Python: >=3.10,<4
|
|
9
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
10
|
+
Classifier: Programming Language :: Python :: 3
|
|
11
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
15
|
+
Requires-Dist: django (>=5)
|
|
16
|
+
Requires-Dist: eval-type-backport (>=0.2.2)
|
|
17
|
+
Requires-Dist: pydantic (>=2.10.6)
|
|
18
|
+
Requires-Dist: typeguard (>=4.0)
|
|
19
|
+
Description-Content-Type: text/markdown
|
|
20
|
+
|
|
21
|
+
# pydantic-djmodel
|
|
22
|
+
|
|
23
|
+
[](https://python-poetry.org/)
|
|
24
|
+
[](https://github.com/astral-sh/ruff)[](https://github.com/PyCQA/bandit)
|
|
25
|
+

|
|
26
|
+

|
|
27
|
+
[](https://github.com/astral-sh/ruff)
|
|
28
|
+
[](https://pycqa.github.io/isort/)
|
|
29
|
+

|
|
30
|
+

|
|
31
|
+

|
|
32
|
+

|
|
33
|
+
[](https://github.com/pre-commit/pre-commit)
|
|
34
|
+

|
|
35
|
+
|
|
36
|
+
pydantic-djmodel is a Python library that converts Pydantic BaseModels into Django ORM models. It maps Pydantic fields, validators, and constraints to Django model fields, supporting nested models and relationships. This simplifies data validation and database management, reducing code duplication and easing migrations.
|
|
37
|
+
|
|
38
|
+
- Software License: MIT
|
|
39
|
+
|
|
40
|
+
- Documentation: https://pydantic-djmodel.com
|
|
41
|
+
|
|
42
|
+
## Features
|
|
43
|
+
|
|
44
|
+
TBD
|
|
45
|
+
|
|
46
|
+
## Credits
|
|
47
|
+
|
|
48
|
+
This package was created with
|
|
49
|
+
[scicookie](https://github.com/osl-incubator/scicookie) project template.
|
|
50
|
+
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# pydantic-djmodel
|
|
2
|
+
|
|
3
|
+
[](https://python-poetry.org/)
|
|
4
|
+
[](https://github.com/astral-sh/ruff)[](https://github.com/PyCQA/bandit)
|
|
5
|
+

|
|
6
|
+

|
|
7
|
+
[](https://github.com/astral-sh/ruff)
|
|
8
|
+
[](https://pycqa.github.io/isort/)
|
|
9
|
+

|
|
10
|
+

|
|
11
|
+

|
|
12
|
+

|
|
13
|
+
[](https://github.com/pre-commit/pre-commit)
|
|
14
|
+

|
|
15
|
+
|
|
16
|
+
pydantic-djmodel is a Python library that converts Pydantic BaseModels into Django ORM models. It maps Pydantic fields, validators, and constraints to Django model fields, supporting nested models and relationships. This simplifies data validation and database management, reducing code duplication and easing migrations.
|
|
17
|
+
|
|
18
|
+
- Software License: MIT
|
|
19
|
+
|
|
20
|
+
- Documentation: https://pydantic-djmodel.com
|
|
21
|
+
|
|
22
|
+
## Features
|
|
23
|
+
|
|
24
|
+
TBD
|
|
25
|
+
|
|
26
|
+
## Credits
|
|
27
|
+
|
|
28
|
+
This package was created with
|
|
29
|
+
[scicookie](https://github.com/osl-incubator/scicookie) project template.
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "pydantic-djmodel"
|
|
3
|
+
version = "0.1.0" # semantic-release
|
|
4
|
+
description = "pydantic-djmodel is a Python library that converts Pydantic BaseModels into Django ORM models."
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
authors = [
|
|
7
|
+
{name = "Ivan Ogasawara", email = "ivan.ogasawara@gmail.com"}
|
|
8
|
+
]
|
|
9
|
+
license = "MIT"
|
|
10
|
+
requires-python = ">=3.10,<4"
|
|
11
|
+
dependencies = [
|
|
12
|
+
"typeguard (>=4.0)",
|
|
13
|
+
"eval-type-backport (>=0.2.2)",
|
|
14
|
+
"pydantic (>=2.10.6)",
|
|
15
|
+
"django (>=5)",
|
|
16
|
+
]
|
|
17
|
+
|
|
18
|
+
[build-system]
|
|
19
|
+
requires = ["poetry-core>=2", "poetry>=2"]
|
|
20
|
+
build-backend = "poetry.core.masonry.api"
|
|
21
|
+
|
|
22
|
+
[tool.poetry]
|
|
23
|
+
packages = [
|
|
24
|
+
{include = "pydantic_djmodel", from="src"},
|
|
25
|
+
]
|
|
26
|
+
include = ["src/pydantic_djmodel/py.typed"]
|
|
27
|
+
|
|
28
|
+
exclude = [
|
|
29
|
+
".git/*",
|
|
30
|
+
".env*",
|
|
31
|
+
]
|
|
32
|
+
|
|
33
|
+
[tool.poetry.group.dev.dependencies]
|
|
34
|
+
pytest = ">=7.3.2"
|
|
35
|
+
pytest-django = ">=4.10.0,<5.0.0"
|
|
36
|
+
pytest-cov = ">=4.1.0"
|
|
37
|
+
hypothesis = ">=6.0"
|
|
38
|
+
coverage = ">=7.2.7"
|
|
39
|
+
isort = ">=5.12.0"
|
|
40
|
+
pre-commit = ">=3.3.2"
|
|
41
|
+
flake8 = ">=4.0.1, <7"
|
|
42
|
+
ruff = ">=0.2.0"
|
|
43
|
+
mypy = ">=1.5.0"
|
|
44
|
+
bandit = ">=1.7.5"
|
|
45
|
+
pydocstyle = ">=6.3.0"
|
|
46
|
+
vulture = ">=2.7"
|
|
47
|
+
mccabe = ">=0.6.1"
|
|
48
|
+
ipython = "<8"
|
|
49
|
+
ipykernel = ">=6.0.0"
|
|
50
|
+
Jinja2 = ">=3.1.2"
|
|
51
|
+
mkdocs = ">=1.4.3"
|
|
52
|
+
mkdocs-exclude = ">=1.0.2"
|
|
53
|
+
mkdocs-jupyter = ">=0.24.1"
|
|
54
|
+
mkdocs-literate-nav = ">=0.6.0"
|
|
55
|
+
mkdocs-macros-plugin = ">=0.7.0,<1"
|
|
56
|
+
mkdocstrings = ">=0.21.2"
|
|
57
|
+
mkdocstrings-python = ">= 1.1.2"
|
|
58
|
+
mkdocs-gen-files = ">=0.5.0"
|
|
59
|
+
makim = "1.20.0"
|
|
60
|
+
# 'PosixPath' object has no attribute 'endswith'
|
|
61
|
+
virtualenv = "<=20.25.1"
|
|
62
|
+
fhir-resources = ">=8"
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
[tool.bandit]
|
|
66
|
+
exclude_dirs = ["tests"]
|
|
67
|
+
targets = "./"
|
|
68
|
+
|
|
69
|
+
[tool.isort]
|
|
70
|
+
ensure_newline_before_comments = true
|
|
71
|
+
line_length = 79
|
|
72
|
+
multi_line_output = 3
|
|
73
|
+
include_trailing_comma = true
|
|
74
|
+
skip_glob = ["docs/*", "*.egg-info"]
|
|
75
|
+
|
|
76
|
+
[tool.mypy]
|
|
77
|
+
python_version = "3.9"
|
|
78
|
+
check_untyped_defs = true
|
|
79
|
+
strict = true
|
|
80
|
+
ignore_missing_imports = true
|
|
81
|
+
warn_unused_ignores = true
|
|
82
|
+
warn_redundant_casts = true
|
|
83
|
+
warn_unused_configs = true
|
|
84
|
+
exclude = ["scripts/"]
|
|
85
|
+
|
|
86
|
+
[tool.pytest.ini_options]
|
|
87
|
+
testpaths = [
|
|
88
|
+
"tests",
|
|
89
|
+
]
|
|
90
|
+
DJANGO_SETTINGS_MODULE = "pydantic_djmodel._settings"
|
|
91
|
+
|
|
92
|
+
[tool.ruff]
|
|
93
|
+
line-length = 79
|
|
94
|
+
force-exclude = true
|
|
95
|
+
src = ["./"]
|
|
96
|
+
exclude = [
|
|
97
|
+
'docs',
|
|
98
|
+
]
|
|
99
|
+
fix = true
|
|
100
|
+
|
|
101
|
+
[tool.ruff.lint]
|
|
102
|
+
select = [
|
|
103
|
+
"E", # pycodestyle
|
|
104
|
+
"F", # pyflakes
|
|
105
|
+
"D", # pydocstyle
|
|
106
|
+
"YTT", # flake8-2020
|
|
107
|
+
"RUF", # Ruff-specific rules
|
|
108
|
+
"I001", # isort
|
|
109
|
+
"I", # Enables import sorting checks
|
|
110
|
+
]
|
|
111
|
+
fixable = ["I001"]
|
|
112
|
+
|
|
113
|
+
[tool.ruff.lint.pydocstyle]
|
|
114
|
+
convention = "numpy"
|
|
115
|
+
|
|
116
|
+
[tool.ruff.lint.isort]
|
|
117
|
+
lines-between-types = 1
|
|
118
|
+
|
|
119
|
+
[tool.ruff.format]
|
|
120
|
+
quote-style = "single"
|
|
121
|
+
|
|
122
|
+
[tool.vulture]
|
|
123
|
+
min_confidence = 80
|
|
124
|
+
paths = ["./"]
|
|
125
|
+
sort_by_size = true
|
|
126
|
+
verbose = false
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"""pydantic-djmodel."""
|
|
2
|
+
|
|
3
|
+
from importlib import metadata as importlib_metadata
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
def get_version() -> str:
|
|
7
|
+
"""Return the program version."""
|
|
8
|
+
try:
|
|
9
|
+
return importlib_metadata.version(__name__)
|
|
10
|
+
except importlib_metadata.PackageNotFoundError: # pragma: no cover
|
|
11
|
+
return '0.1.0' # semantic-release
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
version = get_version()
|
|
15
|
+
|
|
16
|
+
__version__ = version
|
|
17
|
+
__author__ = 'Ivan Ogasawara'
|
|
18
|
+
__email__ = 'ivan.ogasawara@gmail.com'
|
|
File without changes
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
"""Main module."""
|
|
2
|
+
|
|
3
|
+
import re
|
|
4
|
+
import sys
|
|
5
|
+
|
|
6
|
+
from pathlib import Path
|
|
7
|
+
from typing import Union, get_args, get_origin
|
|
8
|
+
|
|
9
|
+
import django
|
|
10
|
+
|
|
11
|
+
from django.db import models
|
|
12
|
+
from pydantic import BaseModel
|
|
13
|
+
|
|
14
|
+
# settings.configure(default_settings="seconddx.settings", DEBUG=True)
|
|
15
|
+
django.setup()
|
|
16
|
+
|
|
17
|
+
secondddx_path = Path('.').absolute().parent
|
|
18
|
+
sys.path.insert(0, str(secondddx_path))
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def camel_to_snake(name: str) -> str:
|
|
22
|
+
"""
|
|
23
|
+
Convert a camelCase or PascalCase string to snake_case.
|
|
24
|
+
|
|
25
|
+
Parameters
|
|
26
|
+
----------
|
|
27
|
+
name : str
|
|
28
|
+
The string to convert.
|
|
29
|
+
|
|
30
|
+
Returns
|
|
31
|
+
-------
|
|
32
|
+
str
|
|
33
|
+
The converted string.
|
|
34
|
+
|
|
35
|
+
Notes
|
|
36
|
+
-----
|
|
37
|
+
This function is not idempotent. If the input string is already in
|
|
38
|
+
snake_case, running it through this function will make it lower case.
|
|
39
|
+
|
|
40
|
+
Examples
|
|
41
|
+
--------
|
|
42
|
+
>>> camel_to_snake("camelCaseExample")
|
|
43
|
+
"camel_case_example"
|
|
44
|
+
>>> camel_to_snake("PascalCaseExample")
|
|
45
|
+
"pascal_case_example"
|
|
46
|
+
"""
|
|
47
|
+
name = re.sub( # pyright: ignore[reportGeneralTypeIssues]
|
|
48
|
+
r'(?<!^)(?=[A-Z])', '_', name
|
|
49
|
+
) # Insert _ before uppercase letters (excluding the first)
|
|
50
|
+
return name.lower()
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def pydantic_to_django(
|
|
54
|
+
pydantic_model: BaseModel, model_name: str, module_name: str = 'seconddx'
|
|
55
|
+
) -> type:
|
|
56
|
+
"""
|
|
57
|
+
Dynamically generates a Django model class from a Pydantic model.
|
|
58
|
+
|
|
59
|
+
Parameters
|
|
60
|
+
----------
|
|
61
|
+
pydantic_model : BaseModel
|
|
62
|
+
The Pydantic model to generate a Django model from.
|
|
63
|
+
model_name : str
|
|
64
|
+
The name to give the generated Django model class.
|
|
65
|
+
module_name : str, optional
|
|
66
|
+
The module name to use for the generated Django model.
|
|
67
|
+
Defaults to 'seconddx'.
|
|
68
|
+
|
|
69
|
+
Returns
|
|
70
|
+
-------
|
|
71
|
+
type
|
|
72
|
+
The generated Django model class.
|
|
73
|
+
"""
|
|
74
|
+
fields = {}
|
|
75
|
+
|
|
76
|
+
for field_name, field_info in pydantic_model.model_fields.items():
|
|
77
|
+
field_type = field_info.annotation
|
|
78
|
+
|
|
79
|
+
field_name = camel_to_snake(field_name)
|
|
80
|
+
|
|
81
|
+
# Handle Union types (Optional fields)
|
|
82
|
+
if get_origin(field_type) is Union:
|
|
83
|
+
field_type = get_args(field_type)[0] # Take the first valid type
|
|
84
|
+
|
|
85
|
+
# Map Pydantic types to Django ORM fields
|
|
86
|
+
if field_type is str:
|
|
87
|
+
fields[field_name] = models.CharField(
|
|
88
|
+
max_length=255, blank=True, null=True
|
|
89
|
+
)
|
|
90
|
+
elif field_type is int:
|
|
91
|
+
fields[field_name] = models.IntegerField(blank=True, null=True)
|
|
92
|
+
elif field_type is float:
|
|
93
|
+
fields[field_name] = models.FloatField(blank=True, null=True)
|
|
94
|
+
elif field_type is bool:
|
|
95
|
+
fields[field_name] = models.BooleanField(default=False)
|
|
96
|
+
elif get_origin(field_type) is list:
|
|
97
|
+
fields[field_name] = models.JSONField(
|
|
98
|
+
blank=True, null=True
|
|
99
|
+
) # Lists stored as JSON
|
|
100
|
+
else:
|
|
101
|
+
fields[field_name] = models.JSONField(
|
|
102
|
+
blank=True, null=True
|
|
103
|
+
) # Default to JSONField for unknown types
|
|
104
|
+
|
|
105
|
+
# Add common FHIR fields
|
|
106
|
+
# fields["fhir_id"] = models.CharField(
|
|
107
|
+
# max_length=64, unique=True, blank=True, null=True)
|
|
108
|
+
# fields["image"] = models.ImageField(upload_to=f"{
|
|
109
|
+
# model_name.lower()}s/", blank=True, null=True)
|
|
110
|
+
# fields["original_language"] = models.CharField(
|
|
111
|
+
# max_length=10, blank=True, null=True)
|
|
112
|
+
|
|
113
|
+
# Ensure Django recognizes the generated model
|
|
114
|
+
fields['__module__'] = module_name
|
|
115
|
+
|
|
116
|
+
# Create Django model dynamically
|
|
117
|
+
return type(model_name, (models.Model,), fields)
|
|
File without changes
|