Django Rest,如何更新视图集中对象的单个字段?我得到必填字段的 IntegrityError

Django Rest, How to update a single field of the object in viewset? I get IntegrityError for the required fields

现在我有一个很大的目录对象,我想根据一个请求修改它的一个字段。(其余部分将保持不变)。我有必填字段,因此在执行 PATCH 请求时出现此错误。

null value in column "team_id" of relation "Catalogs_catalog" violates not-null constraint
DETAIL:  Failing row contains (ecf8ede4-1438-441b-8e45-152658512d27, null, [], {}, [], Europe/Moscow, t, null, ["a@a.com"]).

我将 viewset 用于目录视图,并向其添加了一个额外的操作,以便我可以发送 PATCH 请求来更新该字段。

Views.py

    @action(methods=['patch'], detail=True, url_path='add_to_whitelist', url_name='add_to_whitelist')
    def add_to_whitelist(self, request, pk=None):
        catalog = self.get_object()
        users_to_add = request.data
        serializer = WhiteListUpdateSerializer(data=users_to_add,  partial=True)
        if serializer.is_valid():
            serializer.save()
            return Response({"message": "Saved the new white list."}, 201)
        return Response({"message":"Error creating white list.", "error": serializer.errors}, 400)

Serializers.py

class WhiteListUpdateSerializer(serializers.ModelSerializer):
    class Meta:
        model = Catalog
        fields = ('whitelist_users',)

partial=True 或重写 partial_update 无效。我是 DRF 的新手,我在这里缺少什么?

改变

WhiteListUpdateSerializer(data=users_to_add,  partial=True)

WhiteListUpdateSerializer(catalog, data=users_to_add,  partial=True)