Package: pyngo
Pyngo :snake:Utils to help integrate pydantic into Django projectsInstallationYou can add pyngo in a few easy steps. First of all, install the dependency:$pipinstallpyngo

--->100%

SuccessfullyinstalledpyngoFeatures 🎉Using Pydantic to Build your Models in Django Project.UsingOpenAPIutilities to build params from a basic model.usingQueryDictModelto buildPydanticmodels from aQueryDictobject.propagate any errors from Pydantic in Django Rest Framework.Tested in Python 3.10 and up.Examples 📚OpenAPIpyngo.openapi_params()can build params from a basic modelfrompydanticimportBaseModelfrompyngoimportopenapi_paramsclassModel(BaseModel):bingo:intprint(openapi_params(Model))pyngo.ParameterDict.requiredis set according to the type of the variablefromtypingimportOptionalfrompydanticimportBaseModelfrompyngoimportopenapi_paramsclassModel(BaseModel):required_param:intoptional_param:Optional[int]print(openapi_params(Model))Other fields can be set through the field’s info:frompydanticimportBaseModel,Fieldfrompyngoimportopenapi_paramsclassWithDescription(BaseModel):described_param:str=Field(description="Hello World Use Me!")classInPath(BaseModel):path_param:str=Field(location="path")classWithDeprecated(BaseModel):deprecated_field:bool=Field(deprecated=True)classWithNoAllowEmpty(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"])Djangopyngo.querydict_to_dict()andpyngo.QueryDictModelare conveniences for building apydantic.BaseModelfrom adjango.QueryDict.fromtypingimportListfromdjango.httpimportQueryDictfrompydanticimportBaseModelfrompyngoimportQueryDictModel,querydict_to_dictclassModel(BaseModel):single_param:intlist_param:List[str]classQueryModel(QueryDictModel):single_param:intlist_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 Frameworkpyngo.drf_error_details()will propagate any errors from Pydantic.frompydanticimportBaseModel,ValidationErrorfrompyngoimportdrf_error_detailsclassModel(BaseModel):foo:intbar:strdata={"foo":"Cat"}try:Model.parse_obj(data)exceptValidationErrorase:print(drf_error_details(e))Errors descend into nested fields:fromtypingimportListfrompydanticimportBaseModel,ValidationErrorfrompyngoimportdrf_error_detailsclassFramework(BaseModel):frm_id:intclassLanguage(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)exceptValidationErrorase:print(drf_error_details(e))Development 🚧Setup environment 📦You should create a virtual environment and activate it:python-mvenvvenv/sourcevenv/bin/activateAnd then install the development dependencies:# Install dependenciespipinstall-e.[test,lint]Run tests 🌝You can run all the tests with:bashscripts/test.shNote: You can also generate a coverage report with:bashscripts/test_html.shFormat the code 🍂Execute the following command to applypre-commitformatting:bashscripts/format.shExecute the following command to applymypytype checking:bashscripts/lint.shLicense 🍻This project is licensed under the terms of the MIT license.