Skip to content

Add django.Model as an output type #2426

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
56 changes: 49 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# datamodel-code-generator

This code generator creates [pydantic v1 and v2](https://docs.pydantic.dev/) model, [dataclasses.dataclass](https://docs.python.org/3/library/dataclasses.html), [typing.TypedDict](https://docs.python.org/3/library/typing.html#typing.TypedDict)
This code generator creates [pydantic v1 and v2](https://docs.pydantic.dev/) model, [dataclasses.dataclass](https://docs.python.org/3/library/dataclasses.html), [typing.TypedDict](https://docs.python.org/3/library/typing.html#typing.TypedDict)
and [msgspec.Struct](https://github.com/jcrist/msgspec) from an openapi file and others.

[![PyPI version](https://badge.fury.io/py/datamodel-code-generator.svg)](https://pypi.python.org/pypi/datamodel-code-generator)
Expand Down Expand Up @@ -246,6 +246,7 @@ class Apis(BaseModel):
- [dataclasses.dataclass](https://docs.python.org/3/library/dataclasses.html);
- [typing.TypedDict](https://docs.python.org/3/library/typing.html#typing.TypedDict);
- [msgspec.Struct](https://github.com/jcrist/msgspec);
- [django.Model](https://docs.djangoproject.com/en/stable/topics/db/models/);
- Custom type from your [jinja2](https://jinja.palletsprojects.com/en/3.1.x/) template;

## Sponsors
Expand Down Expand Up @@ -273,14 +274,14 @@ class Apis(BaseModel):
</table>

## Projects that use datamodel-code-generator
These OSS projects use datamodel-code-generator to generate many models.

These OSS projects use datamodel-code-generator to generate many models.
See the following linked projects for real world examples and inspiration.

- [airbytehq/airbyte](https://github.com/airbytehq/airbyte)
- *[Generate Python, Java/Kotlin, and Typescript protocol models](https://github.com/airbytehq/airbyte-protocol/tree/main/protocol-models/bin)*
- [apache/iceberg](https://github.com/apache/iceberg)
- *[Generate Python code](https://github.com/apache/iceberg/blob/d2e1094ee0cc6239d43f63ba5114272f59d605d2/open-api/README.md?plain=1#L39)*
- *[Generate Python code](https://github.com/apache/iceberg/blob/d2e1094ee0cc6239d43f63ba5114272f59d605d2/open-api/README.md?plain=1#L39)*
*[`make generate`](https://github.com/apache/iceberg/blob/d2e1094ee0cc6239d43f63ba5114272f59d605d2/open-api/Makefile#L24-L34)*
- [argoproj-labs/hera](https://github.com/argoproj-labs/hera)
- *[`Makefile`](https://github.com/argoproj-labs/hera/blob/c8cbf0c7a676de57469ca3d6aeacde7a5e84f8b7/Makefile#L53-L62)*
Expand All @@ -301,7 +302,7 @@ See the following linked projects for real world examples and inspiration.
- [open-metadata/OpenMetadata](https://github.com/open-metadata/OpenMetadata)
- *[Makefile](https://github.com/open-metadata/OpenMetadata/blob/main/Makefile)*
- [PostHog/posthog](https://github.com/PostHog/posthog)
- *[Generate models via `npm run`](https://github.com/PostHog/posthog/blob/e1a55b9cb38d01225224bebf8f0c1e28faa22399/package.json#L41)*
- *[Generate models via `npm run`](https://github.com/PostHog/posthog/blob/e1a55b9cb38d01225224bebf8f0c1e28faa22399/package.json#L41)*
- [SeldonIO/MLServer](https://github.com/SeldonIO/MLServer)
- *[generate-types.sh](https://github.com/SeldonIO/MLServer/blob/master/hack/generate-types.sh)*

Expand Down Expand Up @@ -338,14 +339,55 @@ $ datamodel-codegen --url https://<INPUT FILE URL> --output model.py
```
This method needs the [http extra option](#http-extra-option)

### Django Model Generation
You can generate Django models from JSON Schema or OpenAPI specifications:

```bash
$ datamodel-codegen --input schema.json --output-model-type django.Model --output models.py
```

Example input (`user_schema.json`):
```json
{
"type": "object",
"properties": {
"id": {"type": "integer"},
"username": {"type": "string"},
"email": {"type": "string", "format": "email"},
"is_active": {"type": "boolean", "default": true},
"created_at": {"type": "string", "format": "date-time"}
},
"required": ["id", "username", "email"]
}
```

Generated Django model:
```python
from django.db import models

class User(models.Model):
id = models.IntegerField()
username = models.CharField(max_length=255)
email = models.EmailField(max_length=255)
is_active = models.BooleanField(null=True, blank=True, default=True)
created_at = models.DateTimeField(null=True, blank=True)
```

The Django model generator automatically:
- Maps JSON Schema types to appropriate Django field types
- Detects email fields and uses `EmailField`
- Detects datetime fields and uses `DateTimeField`
- Handles required vs optional fields with `null=True, blank=True`
- Sets appropriate field options like `max_length` for character fields


## All Command Options

The `datamodel-codegen` command:

<!-- start command help -->
```bash
usage:
usage:
datamodel-codegen [options]

Generate Python data models from schema definitions or structured data
Expand All @@ -369,7 +411,7 @@ Options:
--input-file-type {auto,openapi,jsonschema,json,yaml,dict,csv,graphql}
Input file type (default: auto)
--output OUTPUT Output file (default: stdout)
--output-model-type {pydantic.BaseModel,pydantic_v2.BaseModel,dataclasses.dataclass,typing.TypedDict,msgspec.Struct}
--output-model-type {pydantic.BaseModel,pydantic_v2.BaseModel,dataclasses.dataclass,typing.TypedDict,msgspec.Struct,django.Model}
Output model type (default: pydantic.BaseModel)
--url URL Input file URL. `--input` is ignored when `--url` is used

Expand Down
9 changes: 5 additions & 4 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ class Apis(BaseModel):
- [dataclasses.dataclass](https://docs.python.org/3/library/dataclasses.html);
- [typing.TypedDict](https://docs.python.org/3/library/typing.html#typing.TypedDict);
- [msgspec.Struct](https://github.com/jcrist/msgspec);
- [django.Model](https://docs.djangoproject.com/en/stable/topics/db/models/);
- Custom type from your [jinja2](https://jinja.palletsprojects.com/en/3.1.x) template;

## Sponsors
Expand Down Expand Up @@ -269,13 +270,13 @@ class Apis(BaseModel):
</table>
## Projects that use datamodel-code-generator

These OSS projects use datamodel-code-generator to generate many models.
These OSS projects use datamodel-code-generator to generate many models.
See the following linked projects for real world examples and inspiration.

- [airbytehq/airbyte](https://github.com/airbytehq/airbyte)
- *[code-generator/Dockerfile](https://github.com/airbytehq/airbyte/blob/master/tools/code-generator/Dockerfile)*
- [apache/iceberg](https://github.com/apache/iceberg)
- *[Generate Python code](https://github.com/apache/iceberg/blob/d2e1094ee0cc6239d43f63ba5114272f59d605d2/open-api/README.md?plain=1#L39)*
- *[Generate Python code](https://github.com/apache/iceberg/blob/d2e1094ee0cc6239d43f63ba5114272f59d605d2/open-api/README.md?plain=1#L39)*
*[`make generate`](https://github.com/apache/iceberg/blob/d2e1094ee0cc6239d43f63ba5114272f59d605d2/open-api/Makefile#L24-L34)*
- [argoproj-labs/hera](https://github.com/argoproj-labs/hera)
- *[`Makefile`](https://github.com/argoproj-labs/hera/blob/c8cbf0c7a676de57469ca3d6aeacde7a5e84f8b7/Makefile#L53-L62)*
Expand All @@ -294,7 +295,7 @@ See the following linked projects for real world examples and inspiration.
- [open-metadata/OpenMetadata](https://github.com/open-metadata/OpenMetadata)
- *[Makefile](https://github.com/open-metadata/OpenMetadata/blob/main/Makefile)*
- [PostHog/posthog](https://github.com/PostHog/posthog)
- *[Generate models via `npm run`](https://github.com/PostHog/posthog/blob/e1a55b9cb38d01225224bebf8f0c1e28faa22399/package.json#L41)*
- *[Generate models via `npm run`](https://github.com/PostHog/posthog/blob/e1a55b9cb38d01225224bebf8f0c1e28faa22399/package.json#L41)*
- [SeldonIO/MLServer](https://github.com/SeldonIO/MLServer)
- *[generate-types.sh](https://github.com/SeldonIO/MLServer/blob/master/hack/generate-types.sh)*

Expand Down Expand Up @@ -337,7 +338,7 @@ This method needs the [http extra option](#http-extra-option)
The `datamodel-codegen` command:
<!-- start command help -->
```bash
usage:
usage:
datamodel-codegen [options]

Generate Python data models from schema definitions or structured data
Expand Down
1 change: 1 addition & 0 deletions src/datamodel_code_generator/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ class DataModelType(Enum):
DataclassesDataclass = "dataclasses.dataclass"
TypingTypedDict = "typing.TypedDict"
MsgspecStruct = "msgspec.Struct"
DjangoModel = "django.Model"


class OpenAPIScope(Enum):
Expand Down
11 changes: 10 additions & 1 deletion src/datamodel_code_generator/model/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
) -> DataModelSet:
from datamodel_code_generator import DataModelType # noqa: PLC0415

from . import dataclass, msgspec, pydantic, pydantic_v2, rootmodel, typed_dict # noqa: PLC0415
from . import dataclass, django, msgspec, pydantic, pydantic_v2, rootmodel, typed_dict # noqa: PLC0415
from .types import DataTypeManager # noqa: PLC0415

if target_datetime_class is None:
Expand Down Expand Up @@ -83,6 +83,15 @@
dump_resolve_reference_action=None,
known_third_party=["msgspec"],
)
if data_model_type == DataModelType.DjangoModel:
return DataModelSet(

Check warning on line 87 in src/datamodel_code_generator/model/__init__.py

View check run for this annotation

Codecov / codecov/patch

src/datamodel_code_generator/model/__init__.py#L87

Added line #L87 was not covered by tests
data_model=django.DjangoModel,
root_model=rootmodel.RootModel,
field_model=django.DataModelField,
data_type_manager=django.DataTypeManager,
dump_resolve_reference_action=django.dump_resolve_reference_action,
known_third_party=["django"],
)
msg = f"{data_model_type} is unsupported data model type"
raise ValueError(msg) # pragma: no cover

Expand Down
Loading
Loading