|
14 | 14 | | [`--http-ignore-tls`](#http-ignore-tls) | Disable TLS certificate verification for HTTPS requests. | |
15 | 15 | | [`--http-query-parameters`](#http-query-parameters) | Add query parameters to HTTP requests for remote schemas. | |
16 | 16 | | [`--ignore-pyproject`](#ignore-pyproject) | Ignore pyproject.toml configuration file. | |
| 17 | +| [`--module-split-mode`](#module-split-mode) | Split generated models into separate files, one per model cl... | |
17 | 18 | | [`--shared-module-name`](#shared-module-name) | Customize the name of the shared module for deduplicated mod... | |
18 | 19 | | [`--watch`](#watch) | Watch mode cannot be used with --check mode. | |
19 | 20 | | [`--watch-delay`](#watch-delay) | Watch mode starts file watcher and handles clean exit. | |
@@ -1672,6 +1673,116 @@ testing without project configuration. |
1672 | 1673 |
|
1673 | 1674 | --- |
1674 | 1675 |
|
| 1676 | +## `--module-split-mode` {#module-split-mode} |
| 1677 | + |
| 1678 | +Split generated models into separate files, one per model class. |
| 1679 | + |
| 1680 | +The `--module-split-mode=single` flag generates each model class in its own file, |
| 1681 | +named after the class in snake_case. Use with `--all-exports-scope=recursive` to |
| 1682 | +create an __init__.py that re-exports all models for convenient imports. |
| 1683 | + |
| 1684 | +**Related:** [`--all-exports-scope`](general-options.md#all-exports-scope), [`--use-exact-imports`](template-customization.md#use-exact-imports) |
| 1685 | + |
| 1686 | +!!! tip "Usage" |
| 1687 | + |
| 1688 | + ```bash |
| 1689 | + datamodel-codegen --input schema.json --module-split-mode single --all-exports-scope recursive --use-exact-imports # (1)! |
| 1690 | + ``` |
| 1691 | + |
| 1692 | + 1. :material-arrow-left: `--module-split-mode` - the option documented here |
| 1693 | + |
| 1694 | +??? example "Input Schema" |
| 1695 | + |
| 1696 | + ```json |
| 1697 | + { |
| 1698 | + "$schema": "http://json-schema.org/draft-07/schema#", |
| 1699 | + "definitions": { |
| 1700 | + "User": { |
| 1701 | + "type": "object", |
| 1702 | + "properties": { |
| 1703 | + "id": {"type": "integer"}, |
| 1704 | + "name": {"type": "string"} |
| 1705 | + } |
| 1706 | + }, |
| 1707 | + "Order": { |
| 1708 | + "type": "object", |
| 1709 | + "properties": { |
| 1710 | + "id": {"type": "integer"}, |
| 1711 | + "user": {"$ref": "#/definitions/User"} |
| 1712 | + } |
| 1713 | + } |
| 1714 | + } |
| 1715 | + } |
| 1716 | + ``` |
| 1717 | + |
| 1718 | +??? example "Output" |
| 1719 | + |
| 1720 | + ```python |
| 1721 | + # __init__.py |
| 1722 | + # generated by datamodel-codegen: |
| 1723 | + # filename: input.json |
| 1724 | + |
| 1725 | + from __future__ import annotations |
| 1726 | + |
| 1727 | + from .model import Model |
| 1728 | + from .order import Order |
| 1729 | + from .user import User |
| 1730 | + |
| 1731 | + __all__ = [ |
| 1732 | + "Model", |
| 1733 | + "Order", |
| 1734 | + "User", |
| 1735 | + ] |
| 1736 | + |
| 1737 | + # model.py |
| 1738 | + # generated by datamodel-codegen: |
| 1739 | + # filename: input.json |
| 1740 | + |
| 1741 | + from __future__ import annotations |
| 1742 | + |
| 1743 | + from typing import Any |
| 1744 | + |
| 1745 | + from pydantic import BaseModel |
| 1746 | + |
| 1747 | + |
| 1748 | + class Model(BaseModel): |
| 1749 | + __root__: Any |
| 1750 | + |
| 1751 | + # order.py |
| 1752 | + # generated by datamodel-codegen: |
| 1753 | + # filename: input.json |
| 1754 | + |
| 1755 | + from __future__ import annotations |
| 1756 | + |
| 1757 | + from typing import Optional |
| 1758 | + |
| 1759 | + from pydantic import BaseModel |
| 1760 | + |
| 1761 | + from .user import User |
| 1762 | + |
| 1763 | + |
| 1764 | + class Order(BaseModel): |
| 1765 | + id: Optional[int] = None |
| 1766 | + user: Optional[User] = None |
| 1767 | + |
| 1768 | + # user.py |
| 1769 | + # generated by datamodel-codegen: |
| 1770 | + # filename: input.json |
| 1771 | + |
| 1772 | + from __future__ import annotations |
| 1773 | + |
| 1774 | + from typing import Optional |
| 1775 | + |
| 1776 | + from pydantic import BaseModel |
| 1777 | + |
| 1778 | + |
| 1779 | + class User(BaseModel): |
| 1780 | + id: Optional[int] = None |
| 1781 | + name: Optional[str] = None |
| 1782 | + ``` |
| 1783 | + |
| 1784 | +--- |
| 1785 | + |
1675 | 1786 | ## `--shared-module-name` {#shared-module-name} |
1676 | 1787 |
|
1677 | 1788 | Customize the name of the shared module for deduplicated models. |
|
0 commit comments