def patch(self, request, pk): cardmodel = self.get_object(pk) serializer = CardSerializer( cardmodel, data=request.data, partial=True) # set partial=True to update a data partially if serializer.is_valid(): serializer.save() return Response(code=201, data=serializer.data) return Response(code=400, data="wrong parameters")
def post(self, request, format=None): data = request.data serializer = CardSerializer(data=data) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_201_CREATED) # Provide error message code with request header request.META['error_message'] = 'BAD_REQUEST'
def test_create_card(self, expansion_1): values_error = {"expansion": {"id": 9}, "name": "Vellen", "cost": 2} serializer = CardSerializer(data=values_error) serializer.is_valid() with pytest.raises(Expansion.DoesNotExist): serializer.save() values = { "expansion": { "id": expansion_1.id }, "name": "Vellen", "cost": 2 } serializer = CardSerializer(data=values) assert serializer.is_valid() serializer.save() assert Card.objects.filter(name=values["name"])
def put(self, request, pk, format=None): card = self.get_object(pk) current_card_author = card.author.lower() data = request.data serializer = CardSerializer(card, data=data) if serializer.is_valid(): # Check author field in request data and see whether it is provided or not. if 'author' in data: incoming_card_author = data['author'].lower() # Check card's author whether it is correct or not. if incoming_card_author == current_card_author: serializer.save() return Response(serializer.data) else: request.META['error_message'] = 'FORBIDDEN' return request.META['error_message'] = 'BAD_REQUEST'