Skip to content

Commit

Permalink
Rebuild serializer on model refs update
Browse files Browse the repository at this point in the history
  • Loading branch information
JargeZ committed Aug 15, 2024
1 parent b5b8e66 commit d06d5f8
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion src/drf_pydantic/base_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from rest_framework import serializers
from typing_extensions import dataclass_transform

from drf_pydantic.parse import create_serializer_from_model
from drf_pydantic.parse import SERIALIZER_REGISTRY, create_serializer_from_model


@dataclass_transform(kw_only_default=True, field_specifiers=(pydantic.Field,))
Expand Down Expand Up @@ -50,3 +50,27 @@ def __new__(
class BaseModel(pydantic.BaseModel, metaclass=ModelMetaclass):
# Populated by the metaclass or manually set by the user
drf_serializer: ClassVar[type[serializers.Serializer]]

@classmethod
def model_rebuild(
cls,
*,
force: bool = False,
raise_errors: bool = True,
_parent_namespace_depth: int = 2,
_types_namespace: Optional[dict[str, Any]] = None,
) -> bool | None:
ret = super().model_rebuild(
force=force,
raise_errors=raise_errors,
_parent_namespace_depth=_parent_namespace_depth,
_types_namespace=_types_namespace,
)

if not hasattr(cls, "drf_serializer") or getattr(cls, "drf_serializer") in (
getattr(base, "drf_serializer", None) for base in cls.__mro__[1:]
):
SERIALIZER_REGISTRY.pop(cls)
cls.drf_serializer = create_serializer_from_model(cls)

return ret

0 comments on commit d06d5f8

Please sign in to comment.