修改后 to_representation 的 Rest Framework 序列化程序架构
Schema for Rest Framework serializer with modified to_representation
我对序列化程序的 to_representation
方法进行了修改,以便通过
展平嵌套关系
class LocalBaseSerializer(serializers.ModelSerializer):
"""Helper serializer flatenning the data coming from General
Information."""
general_information = GeneralInformationSerializer(read_only=True)
class Meta:
abstract = True
model = None
exclude = ("id", "created_at")
def to_representation(self, instance):
data = super().to_representation(instance)
general_information = data.pop("general_information")
_ = general_information.pop("id")
return {**general_information, **data}
class ContractReadSerializer(LocalBaseSerializer, serializers.ModelSerializer):
class Meta:
model = Contract
exclude = ("created_at",)
它按预期工作,但到目前为止,我没有设法获得正确的架构,如下面的摘录所示
ContractRead:
type: object
description: |-
Helper serializer flatenning the data coming from General
Information.
properties:
id:
type: integer
readOnly: true
general_information:
allOf:
- $ref: '#/components/schemas/GeneralInformation'
readOnly: true
beginning_of_the_contracts:
type: string
format: date
termination_of_the_contracts:
type: string
format: date
required:
- beginning_of_the_contracts
- general_information
- id
- termination_of_the_contracts
我在 DRF 文档和 drf-spectacular
文档中都没有找到任何帮助。
在此先感谢您的帮助。
您可以尝试定义 GeneralInformationSerializer
中使用的字段并使用 source
属性 例如:
class LocalBaseSerializer(serializers.ModelSerializer):
"""Helper serializer flatenning the data coming from General
Information."""
some_field = serializers.CharField(source="general_information.some_field", read_only=True)
#... some other fields like above
class Meta:
abstract = True
model = None
exclude = ("id", "created_at", "some_field")
我对序列化程序的 to_representation
方法进行了修改,以便通过
class LocalBaseSerializer(serializers.ModelSerializer):
"""Helper serializer flatenning the data coming from General
Information."""
general_information = GeneralInformationSerializer(read_only=True)
class Meta:
abstract = True
model = None
exclude = ("id", "created_at")
def to_representation(self, instance):
data = super().to_representation(instance)
general_information = data.pop("general_information")
_ = general_information.pop("id")
return {**general_information, **data}
class ContractReadSerializer(LocalBaseSerializer, serializers.ModelSerializer):
class Meta:
model = Contract
exclude = ("created_at",)
它按预期工作,但到目前为止,我没有设法获得正确的架构,如下面的摘录所示
ContractRead:
type: object
description: |-
Helper serializer flatenning the data coming from General
Information.
properties:
id:
type: integer
readOnly: true
general_information:
allOf:
- $ref: '#/components/schemas/GeneralInformation'
readOnly: true
beginning_of_the_contracts:
type: string
format: date
termination_of_the_contracts:
type: string
format: date
required:
- beginning_of_the_contracts
- general_information
- id
- termination_of_the_contracts
我在 DRF 文档和 drf-spectacular
文档中都没有找到任何帮助。
在此先感谢您的帮助。
您可以尝试定义 GeneralInformationSerializer
中使用的字段并使用 source
属性 例如:
class LocalBaseSerializer(serializers.ModelSerializer):
"""Helper serializer flatenning the data coming from General
Information."""
some_field = serializers.CharField(source="general_information.some_field", read_only=True)
#... some other fields like above
class Meta:
abstract = True
model = None
exclude = ("id", "created_at", "some_field")