Utils to help integrate pydantic into Django projects
You can add pyngo in a few easy steps. First of all, install the dependency:
$ pip install pyngo
---> 100%
Successfully installed pyngo- 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.
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"])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.parse_obj(querydict_to_dict(query_dict, Model)))
print(QueryModel.parse_obj(query_dict))Note: Don't forget to Setup the Django Project.
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.parse_obj(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:
Framework.parse_obj(data)
except ValidationError as e:
print(drf_error_details(e))You should create a virtual environment and activate it:
python -m venv venv/source venv/bin/activateAnd then install the development dependencies:
# Install dependencies
pip install -e .[test,lint]You can run all the tests with:
bash scripts/test.shNote: You can also generate a coverage report with:
bash scripts/test_html.shExecute the following command to apply pre-commit formatting:
bash scripts/format.shExecute the following command to apply mypy type checking:
bash scripts/lint.shThis project is licensed under the terms of the MIT license.