|
1 | | -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: |
| 83 | + |
| 84 | +```bash |
| 85 | +datamodel-codegen --ignore-pyproject --input schema.yaml --output models.py |
| 86 | +``` |
| 87 | + |
| 88 | +## Generating Configuration |
| 89 | + |
| 90 | +Generate a `pyproject.toml` configuration section from CLI arguments: |
| 91 | + |
| 92 | +```bash |
| 93 | +datamodel-codegen --input schema.yaml --output models.py --snake-case-field --generate-pyproject-config |
| 94 | +``` |
| 95 | + |
| 96 | +Output: |
| 97 | + |
| 98 | +```toml |
| 99 | +[tool.datamodel-codegen] |
| 100 | +input = "schema.yaml" |
| 101 | +output = "models.py" |
9 | 102 | snake-case-field = true |
10 | | -strip-default-none = false |
11 | | -target-python-version = "3.7" |
| 103 | +``` |
| 104 | + |
| 105 | +Generate CLI command from existing `pyproject.toml`: |
| 106 | + |
| 107 | +```bash |
| 108 | +datamodel-codegen --generate-cli-command |
| 109 | +``` |
| 110 | + |
| 111 | +With a specific profile: |
| 112 | + |
| 113 | +```bash |
| 114 | +datamodel-codegen --profile api --generate-cli-command |
12 | 115 | ``` |
0 commit comments