Skip to content

Commit

Permalink
fix: some default naming of models/related model schemas
Browse files Browse the repository at this point in the history
Other changes:
- documented the use of @Property methods as field (related to Support for @Property decorated Django model methods #1)
- some other documentation improvements
- removed some obsolete files
  • Loading branch information
jhassine committed Nov 29, 2024
1 parent d2cee7b commit b9fcb76
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 342 deletions.
4 changes: 3 additions & 1 deletion .coveragerc
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@ exclude_also =
if TYPE_CHECKING:
if typing.TYPE_CHECKING:
class .*\bProtocol\):
@(abc\.)?abstractmethod
@(abc\.)?abstractmethod
@(pytest\.|pytest\.mark.\.)?skip
@(pytest\.|pytest\.mark.\.)?xfail
11 changes: 8 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
![PyPI - License](https://img.shields.io/pypi/l/django)
![PyPI - Downloads](https://img.shields.io/pypi/dm/django2pydantic)

# Why

django2pydantic is the most complete Pydantic schemas based on Django models.
Expand All @@ -7,16 +10,18 @@ django2pydantic is the most complete Pydantic schemas based on Django models.
django2pydantic is a library that allows to define Pydantic schemas based on Django database models.

Similar libraries:

- [Djantic](https://jordaneremieff.github.io/djantic/)
- [Django Ninja Schema](https://django-ninja.dev/guides/response/django-pydantic/)
- [Ninja Schema](https://github.com/eadwinCode/ninja-schema)

# Key features

- Supports all Django model fields
- Supports all Django model field types
- Supports @property decorated Django model methods
- Supports all Django model relation fields:
- ForeignKey, OneToOneField, ManyToManyField
- The reverse relations of the above (ManyToOneRel, OneToOneRel, ManyToManyRel)
- ForeignKey, OneToOneField, ManyToManyField
- The reverse relations of the above (ManyToOneRel, OneToOneRel, ManyToManyRel)
- Supports defining nested relations
- Provides as complete OpenAPI schema details as possible

Expand Down
9 changes: 7 additions & 2 deletions django2pydantic/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ def create_pydantic_model(
type[BaseModel]: The Pydantic model.
"""
model_name = model_name or f"{django_model.__name__}Schema"

pydantic_fields: dict[
str,
tuple[
Expand Down Expand Up @@ -237,7 +239,6 @@ def create_pydantic_model(

# Finally, create the Pydantic model:
# https://docs.pydantic.dev/2.9/concepts/models/#dynamic-model-creation
model_name = model_name or f"{django_model.__name__}Schema"
return create_model(
model_name,
__base__=bases,
Expand Down Expand Up @@ -283,7 +284,11 @@ def __new__( # pylint: disable=W0222,C0204
msg = f"model field is required in Meta class for {name}"
raise ValueError(msg)

model_name = getattr(namespace["Meta"], "name", None)
model_name = getattr(
namespace["Meta"],
"name",
f"{model_class.__name__}Schema",
)

return create_pydantic_model(
model_class,
Expand Down
2 changes: 1 addition & 1 deletion django2pydantic/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class Meta:
"""Pydantic configuration."""

name: str | None = None
models: Model | None = None
model: Model | None = None
fields: ClassVar[ModelFields | None] = None

@model_validator(mode="wrap")
Expand Down
58 changes: 38 additions & 20 deletions examples.ipynb

Large diffs are not rendered by default.

149 changes: 0 additions & 149 deletions examples.py

This file was deleted.

166 changes: 0 additions & 166 deletions pyproject.toml

This file was deleted.

0 comments on commit b9fcb76

Please sign in to comment.