Skip to content

SerializerMethodField - dict with known keys #184

@jmaslanka

Description

@jmaslanka

Hey, let's assume we have this field in a serializer:

field = SerializerMethodField()

def get_field(self, obj):
    return {
        "type": "some_type",
        "data": OtherSerializer(obj.objects, many=True).data,
        ...
    }

Is there a way to represent it as:

{
    "type": "some_type",
    "data": {
        <serializer data>
    },
    ....
}

I've tried:

  • using typing.TypedDict as type hint to get_field - results in "Unknown type hint"
  • @extend_schema_field accepts only either serializer or OpenApiTypes. I don't see a way to edit additionalProperties for OpenApiTypes.OBJECT.

Seems like a common problem to me. Any ideas?

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requestfix confirmation pendingissue has been fixed and confirmation from issue reporter is pendinglow priorityissues that would be nice to fix but have low impact

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions