0

Now I have a big catalog object and with one request I want to modify a single field of it.( The rest will remain the same). I have required fields so I get this error when I do PATCH request.

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"]).

I used viewset for catalog views and I added one more extra action to it so I can send a PATCH request to update the field.

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 or rewriting partial_update didn't work. I am new to DRF, What is the thing I am missing here?

mr_nocoding
  • 472
  • 7
  • 16

1 Answers1

2

Change

WhiteListUpdateSerializer(data=users_to_add,  partial=True)

to

WhiteListUpdateSerializer(catalog, data=users_to_add,  partial=True)
itzMEonTV
  • 19,851
  • 4
  • 39
  • 49