最新消息:20190717 VPS服务器:Vultr新加坡,WordPress主题:大前端D8,统一介绍入口:关于

【已解决】Django中序列化一个模型对象中的另外子对象的数组

Django crifan 77浏览 0评论
折腾:
【未解决】剧本编写系统中优化超级管理员的全部剧本统计页
期间,想要获得当前所有的用户组,包括每个用户组中的成员的详细信息,包括用户名和id
目前已通过:
/apps/user/views.py
class FunctionGroupViewSet(mixins.CreateModelMixin,
                           mixins.ListModelMixin,
                           mixins.RetrieveModelMixin,
                           PutOnlyUpdateModelMixin,
                           mixins.DestroyModelMixin,
                           viewsets.GenericViewSet):
    """
        功能组,有组长
    """
    queryset = FunctionGroup.objects.all()
    serializer_class = FunctionGroupSerializer
    permission_classes = (IsAuthenticated, IsUserFunctionAdmin)
    filter_backends = (OrderingFilter, SearchFilter)
    ordering_fields = ('created_at', )
    ordering = ('-created_at', )
    search_fields = ('name', 'description')
apps/user/serializers.py
class FunctionGroupSerializer(serializers.ModelSerializer):
    owner = serializers.CharField(source='owner.username')
    function = serializers.SerializerMethodField()
    class Meta:
        model = FunctionGroup
        # fields = ('id', 'owner', 'name', 'function', 'description',
        #           'created_at', 'updated_at')
        fields = ('id', 'owner', 'name', 'function', 'description', 'members',
                  'created_at', 'updated_at')

        read_only_fields = ('id', 'created_at', 'updated_at')

    def get_function(self, obj):
        return obj.get_function_display()
目前暂时可以获取:
functionGroups= {count: 4, next: null, previous: null, results: Array(4)}count: 4next: nullprevious: nullresults: Array(4)0: {id: 6, owner: "xxx", name: "xxx剧本组", function: "script_function_group", description: "xxx剧本组", …}1: created_at: "2018-07-04 21:41:14"description: "maggie剧本组"function: "script_function_group"id: 5members: Array(3)0: "44c3de7f-a0e4-4a62-8f64-720044ad6d03"1: "78c7b3c4-8a7c-4f1f-96ce-dc469fedad40"2: "7dff71b1-e48f-4b94-ade3-c832742a8ed7"length: 3__proto__: Array(0)name: "maggie剧本组"owner: "Maggie"updated_at: "2018-07-25 16:24:05"__proto__: Object2: {id: 4, owner: "xxx", name: "script_function_group", function: "script_function_group", description: "剧本功能组,请勿改动名称", …}3: {id: 3, owner: "xxx", name: "admin_function_group", function: "admin_function_group", description: "超级管理员功能组", …}length: 4__proto__: Array(0)__proto__: Object
每个functionGroup的member的id了,但是想要获取member的name(和其他信息)
试了试:
# members = UserSerializer(source='members')
members = UserSerializer()
结果没用。
Django serialize model submodel
Serializer relations – Django REST framework
【总结】
最后通过添加:
members = UserSerializer(many=True, read_only=True)
相关完整代码是:
class FunctionGroupSerializer(serializers.ModelSerializer):
    owner = serializers.CharField(source='owner.username')
    function = serializers.SerializerMethodField()
    members = UserSerializer(many=True, read_only=True)

    class Meta:
        model = FunctionGroup
        fields = ('id', 'owner', 'name', 'function', 'description',
            'members', 'created_at', 'updated_at')

        read_only_fields = ('id', 'created_at', 'updated_at')

    def get_function(self, obj):
        return obj.get_function_display()
结果:
就达到要的效果了:

转载请注明:在路上 » 【已解决】Django中序列化一个模型对象中的另外子对象的数组

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
16 queries in 0.086 seconds, using 9.29MB memory