|
25 | 25 | | [`--target-python-version`](#target-python-version) | Target Python version for generated code syntax and imports.... | |
26 | 26 | | [`--union-mode`](#union-mode) | Union mode for combining anyOf/oneOf schemas (smart or left_... | |
27 | 27 | | [`--use-default`](#use-default) | Use default values from schema in generated models. | |
| 28 | +| [`--use-default-factory-for-optional-nested-models`](#use-default-factory-for-optional-nested-models) | Generate default_factory for optional nested model fields. | |
28 | 29 | | [`--use-default-kwarg`](#use-default-kwarg) | Use default= keyword argument instead of positional argument... | |
29 | 30 | | [`--use-frozen-field`](#use-frozen-field) | Generate frozen (immutable) field definitions for readOnly p... | |
30 | 31 | | [`--use-one-literal-as-default`](#use-one-literal-as-default) | Use single literal value as default when enum has only one o... | |
@@ -4834,6 +4835,148 @@ The `--use-default` flag configures the code generation behavior. |
4834 | 4835 |
|
4835 | 4836 | --- |
4836 | 4837 |
|
| 4838 | +## `--use-default-factory-for-optional-nested-models` {#use-default-factory-for-optional-nested-models} |
| 4839 | + |
| 4840 | +Generate default_factory for optional nested model fields. |
| 4841 | + |
| 4842 | +The `--use-default-factory-for-optional-nested-models` flag generates default_factory |
| 4843 | +for optional nested model fields instead of None default: |
| 4844 | +- Dataclasses: `field: Model | None = field(default_factory=Model)` |
| 4845 | +- Pydantic: `field: Model | None = Field(default_factory=Model)` |
| 4846 | +- msgspec: `field: Model | UnsetType = field(default_factory=Model)` |
| 4847 | + |
| 4848 | +!!! tip "Usage" |
| 4849 | + |
| 4850 | + ```bash |
| 4851 | + datamodel-codegen --input schema.json --use-default-factory-for-optional-nested-models # (1)! |
| 4852 | + ``` |
| 4853 | + |
| 4854 | + 1. :material-arrow-left: `--use-default-factory-for-optional-nested-models` - the option documented here |
| 4855 | + |
| 4856 | +??? example "Examples" |
| 4857 | + |
| 4858 | + **Input Schema:** |
| 4859 | + |
| 4860 | + ```json |
| 4861 | + { |
| 4862 | + "$schema": "http://json-schema.org/draft-07/schema#", |
| 4863 | + "type": "object", |
| 4864 | + "properties": { |
| 4865 | + "name": {"type": "string"}, |
| 4866 | + "address": {"$ref": "#/$defs/Address"}, |
| 4867 | + "contact": {"$ref": "#/$defs/Contact"} |
| 4868 | + }, |
| 4869 | + "required": ["name"], |
| 4870 | + "$defs": { |
| 4871 | + "Address": { |
| 4872 | + "type": "object", |
| 4873 | + "properties": { |
| 4874 | + "street": {"type": "string"}, |
| 4875 | + "city": {"type": "string"} |
| 4876 | + } |
| 4877 | + }, |
| 4878 | + "Contact": { |
| 4879 | + "type": "object", |
| 4880 | + "properties": { |
| 4881 | + "email": {"type": "string"}, |
| 4882 | + "phone": {"type": "string"} |
| 4883 | + } |
| 4884 | + } |
| 4885 | + } |
| 4886 | + } |
| 4887 | + ``` |
| 4888 | + |
| 4889 | + **Output:** |
| 4890 | + |
| 4891 | + === "Pydantic v2" |
| 4892 | + |
| 4893 | + ```python |
| 4894 | + # generated by datamodel-codegen: |
| 4895 | + # filename: default_factory_nested_model.json |
| 4896 | + # timestamp: 2019-07-26T00:00:00+00:00 |
| 4897 | + |
| 4898 | + from __future__ import annotations |
| 4899 | + |
| 4900 | + from pydantic import BaseModel, Field |
| 4901 | + |
| 4902 | + |
| 4903 | + class Address(BaseModel): |
| 4904 | + street: str | None = None |
| 4905 | + city: str | None = None |
| 4906 | + |
| 4907 | + |
| 4908 | + class Contact(BaseModel): |
| 4909 | + email: str | None = None |
| 4910 | + phone: str | None = None |
| 4911 | + |
| 4912 | + |
| 4913 | + class Model(BaseModel): |
| 4914 | + name: str |
| 4915 | + address: Address | None = Field(default_factory=Address) |
| 4916 | + contact: Contact | None = Field(default_factory=Contact) |
| 4917 | + ``` |
| 4918 | + |
| 4919 | + === "dataclass" |
| 4920 | + |
| 4921 | + ```python |
| 4922 | + # generated by datamodel-codegen: |
| 4923 | + # filename: default_factory_nested_model.json |
| 4924 | + # timestamp: 2019-07-26T00:00:00+00:00 |
| 4925 | + |
| 4926 | + from __future__ import annotations |
| 4927 | + |
| 4928 | + from dataclasses import dataclass, field |
| 4929 | + |
| 4930 | + |
| 4931 | + @dataclass |
| 4932 | + class Address: |
| 4933 | + street: str | None = None |
| 4934 | + city: str | None = None |
| 4935 | + |
| 4936 | + |
| 4937 | + @dataclass |
| 4938 | + class Contact: |
| 4939 | + email: str | None = None |
| 4940 | + phone: str | None = None |
| 4941 | + |
| 4942 | + |
| 4943 | + @dataclass |
| 4944 | + class Model: |
| 4945 | + name: str |
| 4946 | + address: Address | None = field(default_factory=Address) |
| 4947 | + contact: Contact | None = field(default_factory=Contact) |
| 4948 | + ``` |
| 4949 | + |
| 4950 | + === "msgspec" |
| 4951 | + |
| 4952 | + ```python |
| 4953 | + # generated by datamodel-codegen: |
| 4954 | + # filename: default_factory_nested_model.json |
| 4955 | + # timestamp: 2019-07-26T00:00:00+00:00 |
| 4956 | + |
| 4957 | + from __future__ import annotations |
| 4958 | + |
| 4959 | + from msgspec import UNSET, Struct, UnsetType, field |
| 4960 | + |
| 4961 | + |
| 4962 | + class Address(Struct): |
| 4963 | + street: str | UnsetType = UNSET |
| 4964 | + city: str | UnsetType = UNSET |
| 4965 | + |
| 4966 | + |
| 4967 | + class Contact(Struct): |
| 4968 | + email: str | UnsetType = UNSET |
| 4969 | + phone: str | UnsetType = UNSET |
| 4970 | + |
| 4971 | + |
| 4972 | + class Model(Struct): |
| 4973 | + name: str |
| 4974 | + address: Address | UnsetType = field(default_factory=Address) |
| 4975 | + contact: Contact | UnsetType = field(default_factory=Contact) |
| 4976 | + ``` |
| 4977 | + |
| 4978 | +--- |
| 4979 | + |
4837 | 4980 | ## `--use-default-kwarg` {#use-default-kwarg} |
4838 | 4981 |
|
4839 | 4982 | Use default= keyword argument instead of positional argument for fields with defaults. |
|
0 commit comments