Scenario: I have Flutter as front end, and django as back end. django-rest-framework is used for managing the API. I have a dataset of customer profile details, which needs to be edited. So from the flutter front end, this data is passed to the backend, which updates the data, and sends a successful message.

My model:

class customer(models.Model):
    # Need autoincrement, unique and primary
    cstid = models.AutoField(primary_key=True, unique=True)
    date_of_registration = models.DateField(default=timezone.now)
    insurance_number = models.CharField(max_length=100, blank=True, null=True)
    name = models.CharField(max_length=35, blank=False)
    ageyrs = models.IntegerField(blank=True)
    agemnths = models.IntegerField(blank=True)
    dob = models.DateField(null=True, blank=True)

I write an APIView, as described in the documentation:

class UpdateCustomerDetail(APIView):
    """
    Retrieve, update or delete a snippet instance.
    For details, refer: https://www.django-rest-framework.org/tutorial/3-class-based-views/
    """
    print("In UpdateCustomerDetail")

    def get_object(self, pk):
        try:
            return customer.objects.get(pk=pk)
        except customer.DoesNotExist:
            raise Http404

    def get(self, request, pk, format=None):
        snippet = self.get_object(pk)
        serializer = customerSerializer(snippet)
        return Response(serializer.data)

    def patch(self, request, pk, format=None):
        cst = self.get_object(pk)
        print("request.data:", request.data)
        customer_data = json.loads(request.data['customer'])
        serializer = customerSerializer(cst, data=customer_data)
        if serializer.is_valid():
            print("Serializer is valid")
            serializer.save()
            print(Response(serializer.data))
            return Response(serializer.data)
        else:
            print("Serializer is NOT valid")
            print(serializer.errors)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

    def delete(self, request, pk, format=None):
        snippet = self.get_object(pk)
        snippet.delete()
        return Response(status=status.HTTP_204_NO_CONTENT)

Now, add a url for it:

    path('update_customer_new/<int:pk>/', views.UpdateCustomerDetail.as_view(),
         name='update_customer_new'),

So, what we are doing, is to pass the pk (which never changes), along with the JSON of a representation, of the model, here, customer, to django.

I am using it from Flutter., So I call the url: /api/update_customer_new/5/

The Flutter code is as follows:

  Future updateCustomerNew({
    int clinicID,
    Customer customer,
  }) async {
    print("In Network::updateCustomer");
    String basicAuth =
        'Basic ' + base64Encode(utf8.encode('$userName:$passWord'));
    print(basicAuth);
    int cstID = customer.cstid;
    var response = await http.patch(
      '${host}update_customer_new/${cstID}/',
      headers: <String, String>{
        'Content-Type': 'application/json; charset=UTF-8',
        'authorization': basicAuth,
      },
      body: jsonEncode(<String, String>{
        'clinicID': clinicID.toString(),
        'customer': jsonEncode(customer),
      }),
    );
    print("Sending: ${jsonEncode(customer)}");
    print('Response status: ${response.statusCode}');
    print(response.body);
    return response.body;
  }

So, django gets the pk, as part of the url, and the data as json. It then calls the APIView, which passes the request to the patch method. The object is retrieved by its pk, so we can update any or all fields on it.