You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
datamodel-code-generator has a lot of command-line options.
1
+
# pyproject.toml Configuration
2
2
3
-
The options are supported on`pyproject.toml`.
3
+
datamodel-code-generator can be configured using `pyproject.toml`. The tool automatically searches for`pyproject.toml` in the current directory and parent directories (stopping at the git repository root).
4
4
5
-
Example `pyproject.toml`:
6
-
```toml
5
+
## Basic Usage
6
+
7
+
```toml
7
8
[tool.datamodel-codegen]
9
+
input = "schema.yaml"
10
+
output = "models.py"
11
+
target-python-version = "3.11"
12
+
snake-case-field = true
8
13
field-constraints = true
14
+
```
15
+
16
+
All CLI options can be used in `pyproject.toml` by converting them to kebab-case (e.g., `--snake-case-field` becomes `snake-case-field`).
17
+
18
+
## Named Profiles
19
+
20
+
You can define multiple named profiles for different use cases within a single project:
21
+
22
+
```toml
23
+
[tool.datamodel-codegen]
24
+
target-python-version = "3.9"
25
+
snake-case-field = true
26
+
27
+
[tool.datamodel-codegen.profiles.api]
28
+
input = "schemas/api.yaml"
29
+
output = "src/models/api.py"
30
+
target-python-version = "3.11"
31
+
32
+
[tool.datamodel-codegen.profiles.database]
33
+
input = "schemas/db.json"
34
+
output = "src/models/db.py"
35
+
input-file-type = "jsonschema"
36
+
```
37
+
38
+
Base settings in `[tool.datamodel-codegen]` are used when no profile is specified, and also serve as defaults for profiles.
39
+
40
+
Use a profile with the `--profile` option:
41
+
42
+
```bash
43
+
datamodel-codegen --profile api
44
+
datamodel-codegen --profile database
45
+
```
46
+
47
+
## Configuration Priority
48
+
49
+
Settings are applied in the following priority order (highest to lowest):
50
+
51
+
1.**CLI arguments** - Always take precedence
52
+
2.**Profile settings** - From `[tool.datamodel-codegen.profiles.<name>]`
53
+
3.**Base settings** - From `[tool.datamodel-codegen]`
54
+
4.**Default values** - Built-in defaults
55
+
56
+
## Merge Rules
57
+
58
+
When using profiles, settings are merged using **shallow merge**:
59
+
60
+
- Profile values **completely replace** base values (no deep merging)
61
+
- Settings not specified in the profile are inherited from the base configuration
62
+
- Lists and dictionaries are replaced entirely, not merged
63
+
64
+
### Example
65
+
66
+
```toml
67
+
[tool.datamodel-codegen]
68
+
strict-types = ["str", "int"]
69
+
http-headers = ["Authorization: Bearer token"]
70
+
71
+
[tool.datamodel-codegen.profiles.api]
72
+
strict-types = ["bytes"]
73
+
```
74
+
75
+
When using `--profile api`:
76
+
77
+
-`strict-types` becomes `["bytes"]` (completely replaces base, not merged)
78
+
-`http-headers` is inherited from base as `["Authorization: Bearer token"]`
79
+
80
+
## Ignoring pyproject.toml
81
+
82
+
To ignore all `pyproject.toml` configuration and use only CLI arguments:
Copy file name to clipboardExpand all lines: pyproject.toml
+1Lines changed: 1 addition & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -208,6 +208,7 @@ filterwarnings = [
208
208
"ignore:^.*black doesn't support `experimental-string-processing` option for wrapping string literal in .*",
209
209
"ignore:^.*jsonschema.exceptions.RefResolutionError is deprecated as of version 4.18.0. If you wish to catch potential reference resolution errors, directly catch referencing.exceptions.Unresolvable..*",
210
210
"ignore:^.*`experimental string processing` has been included in `preview` and deprecated. Use `preview` instead..*",
211
+
"ignore:^.*No schemas found in components/schemas.*",
0 commit comments