georgebv/drf-pydantic

<type> is not a supported composite type.

MrMika96 opened this issue · 5 comments

Hello, i have encountered some strange behavior while trying to use .drf_serializer() method. I have BaseModel what looks like this

class ExampleDTO(BaseModel):
    field_1: Optional[Annotated[int, Field(description="Field1")]] = None
    field_2: Optional[Annotated[int, Field(description="Field2")]] = None
    field_3: Optional[Annotated[str, Field(description="Field3")]] = None

and this return me next error message:

drf_pydantic.errors.ModelConversionError: Error when converting model: ExampleDTO
field_1
     int is not a supported composite type.
field_2
     int is not a supported composite type.
field_3
     str is not a supported composite type.

Can you please suggest some solution?

You are specifying types in the wrong order - Annotated must always come first. In your case it should be Annotated[Optional[int], Field(...)]

Thank you for a quick answer, the error message is gone! But "description" in Field is not displayed, description in my case is necessary because I use .drf_serializer() method to pass serializer to "parameters" in drf_spectacular extended_schema decorator. Can you please help with this too?

@MrMika96 can you provide an example of what you are looking for? I need two models: one is for pydantic and another one is for drf serializer you'd expect it to generate. There is no description field in DRF https://www.django-rest-framework.org/api-guide/fields/

Also keep in mind that you can specify drf serialier fields yourself: https://github.com/georgebv/drf-pydantic?tab=readme-ov-file#manual-serializer-configuration

Certainly. In drf-spectacular you can pass to @extended_schema serializer for responses, request and parameters, like so @extended_schema(parameters=[SomeSerializer]).

class SomeSerializer(serializers.Serializer):
    title = serializers.CharField(required=false, help_text="Filter by title)

In this case help_text will be displayed as a query parameter description in OpenApi.
But if i do @extended_schema(parameters=[SomeDTO.drf_serializer()]).

class SomeDTO(BaseModel):
    title: Annotated[Optional[str], Field(description="Filter by title")] = None

In this case, while converting SomeDTO with drf_serializer, "description" will not be converted into "help_text"