Skip to content
Open
Changes from 2 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
27 changes: 21 additions & 6 deletions fastapi_crudrouter/core/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ def __init__(self, *args, **kwargs) -> None: # type: ignore

def get_pk_type(schema: Type[PYDANTIC_SCHEMA], pk_field: str) -> Any:
try:
return schema.__fields__[pk_field].type_
if int(pydantic_version.split(".")[0]) >= 2:
return schema.model_fields[pk_field].annotation
else:
return schema.__fields__[pk_field].type_
except KeyError:
return int

Expand All @@ -26,11 +29,23 @@ def schema_factory(
Is used to create a CreateSchema which does not contain pk
"""

fields = {
f.name: (f.type_, ...)
for f in schema_cls.__fields__.values()
if f.name != pk_field_name
}
# for handle pydantic 2.x migration
from pydantic import __version__ as pydantic_version

if int(pydantic_version.split(".")[0]) >= 2:
# pydantic 2.x
fields = {
fk: (fv.annotation, ...)
for fk, fv in schema_cls.model_fields.items()
if fk != pk_field_name
}
else:
# pydantic 1.x
fields = {
f.name: (f.type_, ...)
for f in schema_cls.__fields__.values()
if f.name != pk_field_name
}

name = schema_cls.__name__ + name
schema: Type[T] = create_model(__model_name=name, **fields) # type: ignore
Expand Down