Django Rest Framework: Disable save in update

Solution for Django Rest Framework: Disable save in update
is Given Below:

It is my first question here, after reading the similar questions I did not find what I need, thanks for your help.

I am creating a fairly simple API but I want to use best practices at the security level.

Requirement: There is a table in SQL Server with +5 million records that I should ONLY allow READ (all fields) and UPDATE (one field). This is so that a data scientist consumes data from this table and through a predictive model (I think) can assign a value to each record.

For this I mainly need 2 things:

  1. That only one field is updated despite sending all the fields of the table in the Json (I think I have achieved it with my serializer).

  2. And, where I have problems, is in disabling the creation of new records when updating one that does not exist.

I am using an UpdateAPIView to allow trying to allow a bulk update using a json like this (subrrogate_key is in my table and I use lookup_field to:

[
    {
        "subrrogate_key": "A1",
        "class": "A"
    },
   {
        "subrrogate_key": "A2",
        "class": "B"
    },
   {
        "subrrogate_key": "A3",
        "class": "C"
    },
]

When using the partial_update methods use update and this perform_update and this finally calls save and the default operation is to insert a new record if the primary key (or the one specified in lookup_field) is not found.

If I overwrite them, how can I make a new record not be inserted, and only update the field if it exists?

I tried:

Model.objects.filter (subrrogate_key = [‘subrrogate_key’]). Update (class = [‘class])

Model.objects.update_or_create (…)

They work fine if all the keys in the Json exist, because if a new one comes they will insert (I don’t want this).

P.S. I use a translator, sorry.

Django implements that feature through the use of either force_update or update_fields during save().

In some rare circumstances, it’s necessary to be able to force the
save() method to perform an SQL INSERT and not fall back to doing an
UPDATE. Or vice-versa: update, if possible, but not insert a new row.
In these cases you can pass the force_insert=True or force_update=True
parameters to the save() method.

model_obj.save(force_update=True)

or

model_obj.save(update_fields=['field1', 'field2'])

perform_update will create a new record if you passed a serializer that doesn’t have an instance. Depending on how you wrote your view, you can simply check if there is an instance in the serializer before calling save in perform_update to prevent creating a new record:

    def perform_update(self, serializer):
        if not serializer.instance:
            return
        serializer.save()