def put(self, request, pk): menu = self.get_object(pk) serializer = MenuSerializer(menu, data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def update_menu(request, pk): """ 修改指定菜单 --- serializer: menu.serializers.MenuSerializer omit_serializer: false responseMessages: - code: 400 message: Bad Request - code: 404 message: Not Found - code: 401 message: Not authenticated """ try: menu = Menu.objects.get(pk=pk) except Menu.DoesNotExist: return Response('menu type not found', status=status.HTTP_404_NOT_FOUND) serializer = MenuSerializer(menu, data=request.DATA, partial=True) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_202_ACCEPTED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def update_menu(request, pk): """ 修改指定菜单 --- serializer: menu.serializers.MenuSerializer omit_serializer: false responseMessages: - code: 400 message: Bad Request - code: 404 message: Not Found - code: 401 message: Not authenticated """ try: menu = Menu.objects.get(pk=pk) except Menu.DoesNotExist: return Response('menu type not found', status=status.HTTP_404_NOT_FOUND) serializer = MenuSerializer(menu, data=request.DATA, partial=True) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_202_ACCEPTED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def post(self, request): data = request.data serializer = MenuSerializer(data=data) if serializer.is_valid(): serializer.save() # print(serializer.data['id']) return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def post(self, request, format=None): data = request.data.copy() data['user_id'] = request.user.id serializer = MenuSerializer(data=data) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_201_CREATED) #print('SE', serializer.errors) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def menu_list(request, format=None): if request.method == 'GET': menu = Menu.objects.all() serializer = MenuSerializer(menu, many=True) return Response(serializer.data) elif request.method == 'POST': serializer = MenuSerializer(data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def test_validation(self): self.serializer_data['name'] = 'Nazwa update' serializer = MenuSerializer(data=self.serializer_data) serializer.is_valid() new_menu = serializer.save() new_menu.refresh_from_db() self.assertEqual(new_menu.name, 'Nazwa update')
def menu_detail(request, pk, format=None): try: menu = Menu.objects.get(pk=pk) except Menu.DoesNotExist: return Response(status=status.HTTP_404_NOT_FOUND) if request.method == 'GET': serializer = MenuSerializer(menu) return Response(serializer.data) elif request.method == 'PUT': serializer = MenuSerializer(menu, data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) elif request.method == 'DELETE': menu.delete() return Response(status=status.HTTP_204_NO_CONTENT)
def create_menu(request): """ 创建菜单 --- serializer: menu.serializers.MenuSerializer omit_serializer: false responseMessages: - code: 201 message: Created - code: 400 message: Bad Request - code: 401 message: Not authenticated """ data = request.DATA.copy() serializer = MenuSerializer(data=data) if serializer.is_valid(): menu = serializer.save() menu.restaurant = request.current_restaurant menu.save() return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def create_menu(request): """ 创建菜单 --- serializer: menu.serializers.MenuSerializer omit_serializer: false responseMessages: - code: 201 message: Created - code: 400 message: Bad Request - code: 401 message: Not authenticated """ data = request.DATA.copy() serializer = MenuSerializer(data=data) if serializer.is_valid(): menu = serializer.save() menu.restaurant = request.current_restaurant menu.save() return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)