8000 Add datamodel-code-generator link in pydantic document site (#1532) · ag-python/pydantic@6e29848 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6e29848

Browse files
Add datamodel-code-generator link in pydantic document site (pydantic#1532)
* 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>
1 parent 63ec6ff commit 6e29848

File tree

4 files changed

+99
-0
lines changed

4 files changed

+99
-0
lines changed

changes/1500-koxudaxi.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add datamodel-code-generator link in pydantic document site.

docs/datamodel_code_generator.md

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
[datamodel-code-generator](https://github.com/koxudaxi/datamodel-code-generator/) is a command to generate pydantic models from other data types.
2+
3+
4+
* Supported source types
5+
* OpenAPI 3 (YAML/JSON)
6+
* JSON Schema
7+
* JSON/YAML Data (It will be converted to JSON Schema)
8+
9+
## Install
10+
```bash
11+
pip install datamodel-code-generato
12+
```
13+
14+
## Example
15+
In this case, The datamodel-code-generator creates pydantic models from JSON Schema.
16+
```bash
17+
datamodel-codegen --input person.json --input-file-type jsonschema --output model.py
18+
```
19+
20+
person.json:
21+
```json
22+
{
23+
"$id": "person.json",
24+
"$schema": "http://json-schema.org/draft-07/schema#",
25+
"title": "Person",
26+
"type": "object",
27+
"properties": {
28+
"first_name": {
29+
"type": "string",
30+
"description": "The person's first name."
31+
},
32+
"last_name": {
33+
"type": "string",
34+
"description": "The person's last name."
35+
},
36+
"age": {
37+
"description": "Age in years.",
38+
10000 "type": "integer",
39+
"minimum": 0
40+
},
41+
"pets": {
42+
"type": "array",
43+
"items": [
44+
{
45+
"$ref": "#/definitions/Pet"
46+
}
47+
]
48+
},
49+
"comment": {
50+
"type": "null"
51+
}
52+
},
53+
"required": [
54+
"first_name",
55+
"last_name"
56+
],
57+
"definitions": {
58+
"Pet": {
59+
"properties": {
60+
"name": {
61+
"type": "string"
62+
},
63+
"age": {
64+
"type": "integer"
65+
}
66+
}
67+
}
68+
}
69+
}
70+
```
71+
72+
model.py:
73+
```py
74+
{!.tmp_examples/generate_models_person_model.py!}
75+
```
76+
77+
More information can be found on the
78+
[official documentation](https://koxudaxi.github.io/datamodel-code-generator/)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# generated by datamodel-codegen:
2+
# filename: person.json
3+
# timestamp: 2020-05-19T15:07:31+00:00
4+
from __future__ import annotations
5+
from typing import Any, List, Optional
6+
from pydantic import BaseModel, Field, conint
7+
8+
9+
class Pet(BaseModel):
10+
name: Optional[str] = None
11+
age: Optional[int] = None
12+
13+
14+
class Person(BaseModel):
15+
first_name: str = Field(..., description="The person's first name.")
16+
last_name: str = Field(..., description="The person's last name.")
17+
age: Optional[conint(ge=0)] = Field(None, description='Age in years.')
18+
pets: Optional[List[Pet]] = None
19+
comment: Optional[Any] = None

mkdocs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ nav:
4646
- benchmarks.md
4747
- 'Mypy plugin': mypy_plugin.md
4848
- 'PyCharm plugin': pycharm_plugin.md
49+
- 'Code Generation': datamodel_code_generator.md
4950
- changelog.md
5051

5152
markdown_extensions:

0 commit comments

Comments
 (0)
0