从 ViewSet DJANGO 更改对象键

Change Object Key from a ViewSet DJANGO

我想将 object.key 更改为另一个相关值,如下所示:

class SomeViewSet(ModelViewSet):
    queryset = Some.objects.all()
    serialize_class = SomeSerializer
    ....
    def get_queryset(self):
        some_param = self.request.query_params.get("some_param", None)
        if some_param:
            for some in queryset:
                some.fk_another_key = SomeAnother.objects.get(pk=some.fk_another_key.id)

        return queryset

我想要的是将 fk__pk 更改为 "fk object"。我使用 rest-framework,在某些情况下,不能在 Serializer.

中声明 "depth""set object"

目前我的结果json是这样的

{
    ...
    keyX: "",
    keyY: "",
    some_another: 1
    ...
}

我需要这样的东西

{
    ...
    keyX: "",
    keyY: "",
    some_another: {
        ...
        valueX: "",
        valueY: ""
        ...
    }
}

谢谢。

正确的方法是修改序列化程序,以便使用相应的 "SomeAnother" 序列化程序

序列化 some_another 外键
class SomeAnotherSerializer(serializers.HyperlinkedModelSerializer):

    class Meta:
       model = SomeAnother
       fields = ('pk', 'valueX', 'valueY')


class SomeSerializer(serializers.HyperlinkedModelSerializer):

    some_another = SomeAnotherSerializer()

    class Meta:
       model = Some
       fields = ('pk', 'keyX', 'keyY', 'some_another ')