def post(self, request, format=None): assert request.method == "POST" serializer = QModelCustomizationSerializer(data=request.data, context={"request": request}) if serializer.is_valid(): serializer.save() # if I am POSTing new data, then I have created a new customization set, # which means, implicitly, that the set will have a new name # which means that I will redirect to a new page ("www.domain.com/~/new_name) # so use Django's messaging framework to add the success message; # this will automatically get rendered upon the new page load msg = "Successfully saved customization." messages.add_message(request._request, messages.SUCCESS, msg) return Response(serializer.data, status=status.HTTP_201_CREATED) else: return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def put(self, request, pk, format=None): assert request.method == "PUT" model = self.get_object(pk) old_customization_name = model.name serializer = QModelCustomizationSerializer(model, data=request.data, context={"request": request}) if serializer.is_valid(): serializer.save() model.refresh_from_db() if old_customization_name != model.name: # as above, if I've changed the name I will reload the page # this msg automatically get rendered upon the new page load msg = "Successfully saved customization." messages.add_message(request._request, messages.SUCCESS, msg) return Response(serializer.data) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)