def post(self, request): serializer = ContactSerializer(data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_200_OK) else: return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def post(self, request, contact_id=None): if contact_id is not None: contact = Contact.objects.get(id=contact_id) serializer = ContactSerializer(data=request.data) if serializer.is_valid(): contact.first_name = request.data.get("first_name") contact.last_name = request.data.get("last_name") contact.save() return Response(serializer.data, status=status.HTTP_200_OK) else: return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) else: serializer = ContactSerializer(data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_200_OK) else: return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def put(self, request, contact_id): contact = Contact.objects.get(Contact, pk=contact_id) data = request.data serializer = ContactSerializer(data=data) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_204_NO_CONTENT) else: return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def post(self, request): data = request.data data['user_id'] = request.user_id serializer = ContactSerializer(data=data) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_201_CREATED) else: return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def put(self, request, contact_id): contact = Contact.objects.get(id=contact_id) contact.first_name = request.data.get("first_name") contact.last_name = request.data.get("last_name") contact.email = request.data.get("email") contact.phone = request.data.get("phone") contact.address = request.data.get("address") contact.save() serializer = ContactSerializer(data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_200_OK) else: return Response(serializer.data, status=status.HTTP_400_BAD_REQUEST)
def post(self, request, contact_id=None): if contact_id is not None: contact = Contact.objects.filter(id=contact_id) data = request.data contact.update(first_name=data['first_name'], last_name=data['last_name'], email=data['email'], address=data['address'], phone=data['phone']) return Response(status=status.HTTP_200_OK) else: serializer = ContactSerializer(data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_200_OK) else: return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)