Pyngo :snake:
Utils to help integrate pydantic into Django projects
Installation
You can add pyngo in a few easy steps. First of all, install the dependency:
$ pip install pyngo
---> 100%
Successfully installed pyngo
Features 🎉
- Using Pydantic to Build your Models in Django Project.
- Using
OpenAPIutilities to build params from a basic model. - using
QueryDictModelto buildPydanticmodels from aQueryDictobject. - propagate any errors from Pydantic in Django Rest Framework.
- Tested in Python 3.10 and up.
Examples 📚
OpenAPI
pyngo.openapi_params()can build params from a basic model
from pydantic import BaseModel
from pyngo import openapi_params
class Model(BaseModel):
bingo: int
print(openapi_params(Model))
pyngo.ParameterDict.requiredis set according to the type of the variable
from typing import Optional
from pydantic import BaseModel
from pyngo import openapi_params
class Model(BaseModel):
required_param: int
optional_param: Optional[int]
print(openapi_params(Model))
Other fields can be set through the field’s info:
from pydantic import BaseModel, Field
from pyngo import openapi_params
class WithDescription(BaseModel):
described_param: str = Field(
description="Hello World Use Me!"
)
class InPath(BaseModel):
path_param: str = Field(location="path")
class WithDeprecated(BaseModel):
deprecated_field: bool = Field(deprecated=True)
class WithNoAllowEmpty(BaseModel):
can_be_empty: bool = Field(allowEmptyValue=False)
print(openapi_params(WithDescription)[0]["description"])
print(openapi_params(InPath)[0]["in"])
print(openapi_params(WithDeprecated)[0]["deprecated"])
print(openapi_params(WithNoAllowEmpty)[0]["allowEmptyValue"])
Django
pyngo.querydict_to_dict()andpyngo.QueryDictModelare conveniences for building apydantic.BaseModelfrom adjango.QueryDict.
from typing import List
from django.http import QueryDict
from pydantic import BaseModel
from pyngo import QueryDictModel, querydict_to_dict
class Model(BaseModel):
single_param: int
list_param: List[str]
class QueryModel(QueryDictModel):
single_param: int
list_param: List[str]
query_dict = QueryDict("single_param=20&list_param=Life")
print(Model.model_validate(querydict_to_dict(query_dict, Model)))
print(QueryModel.model_validate(query_dict))
Note: Don't forget to Setup the Django Project.
Django Rest Framework
pyngo.drf_error_details()will propagate any errors from Pydantic.
from pydantic import BaseModel, ValidationError
from pyngo import drf_error_details
class Model(BaseModel):
foo: int
bar: str
data = {"foo": "Cat"}
try:
Model.model_validate(data)
except ValidationError as e:
print(drf_error_details(e))
Errors descend into nested fields:
from typing import List
from pydantic import BaseModel, ValidationError
from pyngo import drf_error_details
class Framework(BaseModel):
frm_id: int
class Language(BaseModel):
framework: List[Framework]
data = {"framework": [{"frm_id": "not_a_number"}, {}]}
expected_details = {
"framework": {
"0": {"frm_id": ["value is not a valid integer"]},
"1": {"frm_id": ["field required"]},
}
}
try:
Language.model_validate(data)
except ValidationError as e:
print(drf_error_details(e))
Development 🚧
Setup environment 📦
Install uv from https://docs.astral.sh/uv/ and just from your OS package manager or https://just.systems.
And then install the development dependencies:
# Install dependencies for development
uv sync --all-groups
Run tests 🌝
You can run all the tests with:
just test
Note: You can also generate a coverage report with:
just test-html
Format the code 🍂
Execute the following command to apply pre-commit formatting:
just format
Execute the following command to apply mypy type checking:
just lint
License 🍻
This project is licensed under the terms of the MIT license.
Metadata
Release files for pyngo 2.5.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| pyngo-2.5.0.tar.gz | 46.0 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| pyngo-2.5.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 53.6 kB
Release files / pyngo-2.5.0.tar.gz
| Download URL | pyngo-2.5.0.tar.gz |
|---|---|
| Size | 46.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
6743c522744595cdbb88b1ab8597c9ba89a95a5f9b44b6dfe24243d1102126ba
|
|
BLAKE2b-256 checksum How to use checksums |
6879dc7e90316df252beb34d8b0a7b96a7c7e9ba79348d309c04c5bb35017509
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
Release files / pyngo-2.5.0-py3-none-any.whl
| Download URL | pyngo-2.5.0-py3-none-any.whl |
|---|---|
| Size | 7.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
219cb24f3b26811fc67c032ff7af527ef7041e178eb9e7490c7530ebcfc7b504
|
|
BLAKE2b-256 checksum How to use checksums |
cca32b0dc4ff22384c6130e86470d352516ba91f945f96d2ce5154de4106be7e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
Security for pyngo 2.5.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.