Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
177 changes: 123 additions & 54 deletions docs/cli-reference/field-customization.md
Original file line number Diff line number Diff line change
Expand Up @@ -1339,29 +1339,58 @@ The `--field-extra-keys` flag configures the code generation behavior.

??? example "Output"

```python
# generated by datamodel-codegen:
# filename: extras.json
# timestamp: 2019-07-26T00:00:00+00:00

from __future__ import annotations

from typing import Optional

from pydantic import BaseModel, Field


class Extras(BaseModel):
name: Optional[str] = Field(
None,
description='normal key',
example='example',
invalid_key_1='abc',
key2=456,
repr=True,
)
age: Optional[int] = Field(None, example=12, examples=[13, 20])
```
=== "Pydantic v1"

```python
# generated by datamodel-codegen:
# filename: extras.json
# timestamp: 2019-07-26T00:00:00+00:00

from __future__ import annotations

from typing import Optional

from pydantic import BaseModel, Field


class Extras(BaseModel):
name: Optional[str] = Field(
None,
description='normal key',
example='example',
invalid_key_1='abc',
key2=456,
repr=True,
)
age: Optional[int] = Field(None, example=12, examples=[13, 20])
```

=== "Pydantic v2"

```python
# generated by datamodel-codegen:
# filename: extras.json
# timestamp: 2019-07-26T00:00:00+00:00

from __future__ import annotations

from typing import Optional

from pydantic import BaseModel, Field


class Extras(BaseModel):
name: Optional[str] = Field(
None,
description='normal key',
examples=['example'],
json_schema_extra={'key2': 456, 'invalid-key-1': 'abc'},
repr=True,
)
age: Optional[int] = Field(
None, examples=[13, 20], json_schema_extra={'example': 12}
)
```

---

Expand Down Expand Up @@ -1422,37 +1451,77 @@ in Field(). This is useful for custom schema extensions and vendor-specific meta

??? example "Output"

```python
# generated by datamodel-codegen:
# filename: extras.json
# timestamp: 2019-07-26T00:00:00+00:00

from __future__ import annotations

from typing import Optional

from pydantic import BaseModel, Field


class Extras(BaseModel):
name: Optional[str] = Field(
None,
description='normal key',
example='example',
field_comment='comment',
field_exclude=123,
field_invalid_key_2='efg',
invalid_key_1='abc',
key1=123,
key2=456,
readOnly=True,
register_='hij',
repr=True,
schema_='klm',
x_abc=True,
)
age: Optional[int] = Field(None, example=12, examples=[13, 20], writeOnly=True)
```
=== "Pydantic v1"

```python
# generated by datamodel-codegen:
# filename: extras.json
# timestamp: 2019-07-26T00:00:00+00:00

from __future__ import annotations

from typing import Optional

from pydantic import BaseModel, Field


class Extras(BaseModel):
name: Optional[str] = Field(
None,
description='normal key',
example='example',
field_comment='comment',
field_exclude=123,
field_invalid_key_2='efg',
invalid_key_1='abc',
key1=123,
key2=456,
readOnly=True,
register_='hij',
repr=True,
schema_='klm',
x_abc=True,
)
age: Optional[int] = Field(None, example=12, examples=[13, 20], writeOnly=True)
```

=== "Pydantic v2"

```python
# generated by datamodel-codegen:
# filename: extras.json
# timestamp: 2019-07-26T00:00:00+00:00

from __future__ import annotations

from typing import Optional

from pydantic import BaseModel, Field


class Extras(BaseModel):
name: Optional[str] = Field(
None,
description='normal key',
examples=['example'],
json_schema_extra={
'key1': 123,
'key2': 456,
'$exclude': 123,
'invalid-key-1': 'abc',
'-invalid+key_2': 'efg',
'$comment': 'comment',
'register': 'hij',
'schema': 'klm',
'x-abc': True,
'readOnly': True,
},
repr=True,
)
age: Optional[int] = Field(
None, examples=[13, 20], json_schema_extra={'example': 12, 'writeOnly': True}
)
```

---

Expand Down
Loading
Loading