def put(self, request): uid = request.data['id'] count = Keyword.objects.filter(keyword=request.data['keyword'], category_id=request.data['category_id']).exclude(id=uid).count() if count > 0 : response = { 'code': 0, 'message': 'keyword 已存在', } return Response(response) else: try: keyword = Keyword.objects.get(id=uid) except: response = { 'code': 0, 'message': 'id 不存在', } return Response(response) else: serializer = KeywordSerializer(data=request.data, instance=keyword) if serializer.is_valid(): serializer.save() response = { 'code': 1, 'data': serializer.data, } return Response(response) else: return Response(serializer.errors)
def post(self, request): print(request.data) serializer = KeywordSerializer(data=request.data) if serializer.is_valid(): article = serializer.save() response = { 'code': 1, 'data': serializer.data, } return Response(response) else: return Response(serializer.errors)
def post(self, request): serializer = KeywordSerializer(data=request.data) if serializer.is_valid(): if not Keyword.objects.filter(keyword=request.data['keyword'], category_id=request.data['category_id']): keyword = serializer.save() response = { 'code': 1, 'data': serializer.data, } return Response(response) else: response = { 'code': 0, 'message': '关键字已存在' } return Response(response) else: return Response(serializer.errors)
def put(self, request): uid = request.data['id'] try: article = Keyword.objects.get(id=uid) except: response = { 'code': 0, 'data': [], } return Response(response) else: serializer = KeywordSerializer(data=request.data, instance=article) if serializer.is_valid(): serializer.save() response = { 'code': 1, 'data': serializer.data, } return Response(response) else: return Response(serializer.errors)