You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

54 lines
2.0 KiB

from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status
from apps.goods.models import *
from apps.goods.serializers import *
class GoodsAPIView(APIView):
def get(self, request, *args, **kwargs):
query_data = Goods.objects.all()[:10]
goods_json = GoodsSerializers(query_data, many=True)
print(goods_json.data)
print(goods_json)
r = Response(data=goods_json.data, status=status.HTTP_200_OK)
print(r.data)
print(r.status_code)
print(r)
return Response(data=goods_json.data, status=status.HTTP_200_OK)
def post(self, request, *args, **kwargs):
data = request.data
print(request)
print(data)
goods_serializer = GoodsSerializers(data=data, many=False)
print(goods_serializer)
if goods_serializer.is_valid():
d = goods_serializer.validated_data
print('validated_data', d)
Goods.objects.create(**d)
return Response(goods_serializer.data, status=status.HTTP_200_OK)
else:
return Response(goods_serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def put(self, request, *args, **kwargs):
data = request.data
params = kwargs
print(data)
print(params)
_id = params.get('id')
serializer_goods = GoodsSerializers(data=data, many=False)
if serializer_goods.is_valid():
if goods_obj := Goods.objects.filter(id=_id):
goods_obj.update(**serializer_goods.validated_data)
return Response(serializer_goods.data, status=status.HTTP_200_OK)
return Response(serializer_goods.errors, status=status.HTTP_400_BAD_REQUEST)
def delete(self, request, *args, **kwargs):
del_id = request.data.get('id')
if del_id:
goods_obj = Goods.objects.filter(id=del_id)
goods_obj.delete()
return Response(status=status.HTTP_200_OK)
return Response(status=status.HTTP_400_BAD_REQUEST)