Skip to content

Is there a way to force schema to output a Dict instead of an Array? #395

@pablodgonzalez

Description

@pablodgonzalez

Describe the bug
Hi, I've changed the default list_serializer_class by an DictSerializer but I have not found a way to change the schema according it. Maybe there is some way to do it but i don't know. I tried with extension schemas, and fileds and custom serializerextensions without success.

class BrandDictSerializer(serializers.ModelSerializer)
    Meta:
        model = Brand
        fieds = (id, name)
        list_serializer_class = DictSerializer

class DictSerializer(serializers.ListSerializer):
    dict_key = id

    @property
    def data(self):
        ret = super(serializers.ListSerializer, self).data
        return ReturnDict(ret, serializer=self)

    def to_representation(self, data):
        items = super(DictSerializer, self).to_representation(data)
        return {item[self.dict_key]: item for item in items}

Expected behavior
I expect the schema get an object type with additionalProperties of type Brand

"BrandDic": {
      "type": "object",
      additionalProperties:
      "properties": {
                    "id": {
                        "type": "integer",
                        "readOnly": true
                    },
                    "brand": {
                        "$ref": "#/components/schemas/Brand"
                    }
  }

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingfix confirmation pendingissue has been fixed and confirmation from issue reporter is pending

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions