Handle POST elements in Django Rest Framework

Asked

Viewed 226 times

1

Good night,

I own a model with:

camid = models.AutoField(primary_key=True)

A serializer:

class CameraSerializer(serializers.ModelSerializer):
    class Meta:
        model = Camera
        fields = ["camid", "nome", "path", "tipo", "function"]

And a view:

class CameraList(APIView):
    def post(self, request):
        try:
            serializer = CameraSerializer(data=request.data)
            if serializer.is_valid():
                host = HostsZm.objects.order_by('qusado')[0:1].get()
                if host.qusado < host.qtotal:

                    nome = request.data['nome']
                    path = request.data['path']
                    tipo = request.data['tipo']
                    function = request.data['function']

                    serializer.save(user=self.request.user, host=host.host)

                    zm.add_camwzone("id adicionado via post", nome, "", path, tipo, function, host.host)
                    host.qusado += 1
                    host.save()          
                return Response(serializer.data, status=status.HTTP_201_CREATED) 
            return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
        except Exception:
            return JsonResponse({'Erro:': "Ocorreu um erro no servidor (CLP)"},status=status.HTTP_500_INTERNAL_SERVER_ERROR)

My problem:

I need to send the data entered by the user using the POST. And send them to the "add_camwzone" function in the "zm" file, which will send the data to a third-party API. I can handle the POST data through "request.data[field]".

However, in addition to the data sent by the user, I also need to receive the ID (camid do Model) of the values that were sent by the customer via POST.

Already the field "camid" is generated automatically, I can not get the value through the request.data.

How can I do to redeem this value "camid"?

Example of a request in Postman that returns the ID:

inserir a descrição da imagem aqui

  • Where is the "function add_camwzone"? Is there anything missing from this view or is it complete?

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.