Skip to content

Commit

Permalink
Add datamodel-code-generator link in pydantic document site (#1532)
Browse files Browse the repository at this point in the history
* Add datamodel-code-generator link in pydantic document site.

* tweak menu

* fix blank lines

Co-authored-by: Samuel Colvin <samcolvin@gmail.com>
Co-authored-by: Samuel Colvin <s@muelcolvin.com>
  • Loading branch information
3 people committed May 31, 2020
1 parent 63ec6ff commit 6e29848
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 0 deletions.
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

0 comments on commit 6e29848

Please sign in to comment.