Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add datamodel-code-generator link in pydantic document site #1532

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions changes/1500-koxudaxi.md
@@ -0,0 +1 @@
Add datamodel-code-generator link in pydantic document site.
78 changes: 78 additions & 0 deletions docs/datamodel_code_generator.md
@@ -0,0 +1,78 @@
[datamodel-code-generator](https://github.com/koxudaxi/datamodel-code-generator/) is a command to generate pydantic models from other data types.


* Supported source types
* OpenAPI 3 (YAML/JSON)
* JSON Schema
* JSON/YAML Data (It will be converted to JSON Schema)

## Install
```bash
pip install datamodel-code-generato
```

## Example
In this case, The datamodel-code-generator creates pydantic models from JSON Schema.
```bash
datamodel-codegen --input person.json --input-file-type jsonschema --output model.py
```

person.json:
```json
{
"$id": "person.json",
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Person",
"type": "object",
"properties": {
"first_name": {
"type": "string",
"description": "The person's first name."
},
"last_name": {
"type": "string",
"description": "The person's last name."
},
"age": {
"description": "Age in years.",
"type": "integer",
"minimum": 0
},
"pets": {
"type": "array",
"items": [
{
"$ref": "#/definitions/Pet"
}
]
},
"comment": {
"type": "null"
}
},
"required": [
"first_name",
"last_name"
],
"definitions": {
"Pet": {
"properties": {
"name": {
"type": "string"
},
"age": {
"type": "integer"
}
}
}
}
}
```

model.py:
```py
{!.tmp_examples/generate_models_person_model.py!}
```

More information can be found on the
[official documentation](https://koxudaxi.github.io/datamodel-code-generator/)
19 changes: 19 additions & 0 deletions docs/examples/generate_models_person_model.py
@@ -0,0 +1,19 @@
# generated by datamodel-codegen:
# filename: person.json
# timestamp: 2020-05-19T15:07:31+00:00
from __future__ import annotations
from typing import Any, List, Optional
from pydantic import BaseModel, Field, conint


class Pet(BaseModel):
name: Optional[str] = None
age: Optional[int] = None


class Person(BaseModel):
first_name: str = Field(..., description="The person's first name.")
last_name: str = Field(..., description="The person's last name.")
age: Optional[conint(ge=0)] = Field(None, description='Age in years.')
pets: Optional[List[Pet]] = None
comment: Optional[Any] = None
1 change: 1 addition & 0 deletions mkdocs.yml
Expand Up @@ -46,6 +46,7 @@ nav:
- benchmarks.md
- 'Mypy plugin': mypy_plugin.md
- 'PyCharm plugin': pycharm_plugin.md
- 'Code Generation': datamodel_code_generator.md
- changelog.md

markdown_extensions:
Expand Down