parent
247f0f6722
commit
c1ae3201ea
8 changed files with 118 additions and 4 deletions
@ -0,0 +1,27 @@ |
|||||||
|
from rest_framework.renderers import JSONRenderer |
||||||
|
|
||||||
|
|
||||||
|
class CustomRenderer(JSONRenderer): |
||||||
|
def render(self, data, accepted_media_type=None, renderer_context=None): |
||||||
|
print('render11111111111111111111') |
||||||
|
if renderer_context: |
||||||
|
if isinstance(data, dict): |
||||||
|
msg = data.pop('msg', 'success') |
||||||
|
code = data.pop('code', 200) |
||||||
|
|
||||||
|
if 'status' in data.keys(): |
||||||
|
del data['status'] |
||||||
|
data = data['data'] |
||||||
|
else: |
||||||
|
data = data |
||||||
|
return_data = { |
||||||
|
'msg': msg, |
||||||
|
'code': code, |
||||||
|
'data': { |
||||||
|
'list': data, |
||||||
|
'total': len(data) |
||||||
|
} |
||||||
|
} |
||||||
|
return super().render(return_data, accepted_media_type, renderer_context) |
||||||
|
else: |
||||||
|
return super().render(data, accepted_media_type, renderer_context) |
@ -0,0 +1,4 @@ |
|||||||
|
GET http://127.0.0.1:8000/goods/goods_api_d |
||||||
|
Accept: application/json |
||||||
|
|
||||||
|
### |
@ -0,0 +1,54 @@ |
|||||||
|
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) |
Loading…
Reference in new issue