Reputation: 1211
im trying to update a existing row in db in client side i send request with put method to server i used updatemodelmixin
also id is comming to server in data by this view server response is
maximum recursion depth exceeded
what is correct way to update my data?
class ProductOwnserSingleViewAPIView(APIView,mixins.UpdateModelMixin):
queryset = Product.objects.all()
serializer_class = ProductSerializer
def put(self, request, *args, **kwargs):
return self.put(self,request,*kwargs,**kwargs)
def get(self, request):
id = request.GET.get("id")
try:
product = Product.objects.get(
author_id=request.user.id,
product_id=id
)
if not product:
return Response(status.HTTP_400_BAD_REQUEST)
serializer = ProductSerializer(instance=product, context={"request": request})
return Response(serializer.data)
except Product.DoesNotExist:
return Response(status.HTTP_400_BAD_REQUEST)
Upvotes: 1
Views: 869
Reputation: 476503
You write:
class ProductOwnserSingleViewAPIView(APIView,mixins.UpdateModelMixin):
# ...
def put(self, request, *args, **kwargs):
return self.put(self,request,*kwargs,**kwargs)
In case you perform a PUT
request, then that means that the put(..)
function is called with some arguments. But what does your put
function do? Indeed, it calls itself with the very same parameters. This call subsequently results into another call, so you keep calling until the call stack reaches its maximum, and the the an error occurs.
A typical workflow for a put(..)
is:
class ProductOwnserSingleViewAPIView(APIView,mixins.UpdateModelMixin):
# ...
def put(self, request, pk, format=None):
# obtain the element we want to "update"
product = Product.objects.get(
author_id=request.user.id,
product_id=request.GET('id')
)
# pass the element through the serializer with the data to update
serializer = ProductSerializer(product, data=request.data)
# check if the update makes sense (is valid)
if serializer.is_valid():
# if so, we save the object
serializer.save()
# and return the data as a response
return Response(serializer.data)
# if not, we report the errors as a response
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
So we first obtain the element we wish to PUT
, then we call the serializer that will update the fields that we want to change (data is in the request.data
), and finally in case all those fields are valid we save the object and return all the data of the object.
In case the serializer reports errors, we return those errors as a result.
Upvotes: 1
Reputation: 1116
def put(self, request, *args, **kwargs):
return self.put(self,request,*kwargs,**kwargs)
Change it;
def put(self, request, *args, **kwargs):
return super(ProductOwnserSingleViewAPIView, self).update(request, *args, **kwargs)
Upvotes: 0