Example #1
0
 def patch(self, request, format=None):  # 更新分类
     category = CategorySerializers(Category,
                                    data=request.data,
                                    partial=True)
     if category.is_valid():
         # print(category.validated_data)
         category.save()
         json_data = {"msg": "修改成功", "code": 1}
     else:
         json_data = {"msg": "修改失败", "code": 0}
     return Response(json_data)
Example #2
0
 def post(self, request, format=None):  # 创建分类
     # 反序列化,将json转化成model
     category = CategorySerializers(data=request.data)
     if category.is_valid():
         # 验证分类名重复性
         if Category.objects.filter(
                 category_name=category.validated_data['category_name']
         ).first() is None:
             category.save()
             json_data = {"msg": "添加成功", "code": 1}
         else:
             json_data = {"msg": "已存在该分类", "code": 0}
     else:
         # print(category.errors)
         json_data = {"msg": "添加失败", "code": 0}
     return Response(json_data)
Example #3
0
 def get(self, request, format=None):  # 获取分类列表(分页 + 搜索)
     global category_list
     key = request.GET['key']
     is_parent = request.GET['is_parent']
     parent_id = request.GET.get('parent_id')
     if is_parent == '0':
         if parent_id == '' or parent_id is None:
             category_list = Category.objects.filter(
                 category_name__contains=key, is_parent=0)
         else:
             category_list = Category.objects.filter(
                 category_name__contains=key, parent_id=int(parent_id))
     elif is_parent == '1':
         category_list = Category.objects.filter(
             category_name__contains=key, is_parent=1)
     elif is_parent == '' or is_parent is None:
         category_list = Category.objects.filter(
             category_name__contains=key)
     category_list = category_list.order_by('-gmt_created')
     page = StandardPagination()
     page_sorts = page.paginate_queryset(queryset=category_list,
                                         request=request,
                                         view=self)
     sorts_ser = CategorySerializers(instance=page_sorts, many=True)
     return get_page_response(page, sorts_ser.data)
Example #4
0
def get_child_sorts(request):
    parent_id = request.GET['parent_id']
    parent = Category.objects.get(id=parent_id)
    child_sorts = Category.objects.filter(parent=parent)
    child_sorts_ser = CategorySerializers(instance=child_sorts, many=True)
    return Response(child_sorts_ser.data)
Example #5
0
def get_parent_sorts(request):
    parent_sorts = Category.objects.filter(is_parent=1)
    parent_sorts_ser = CategorySerializers(instance=parent_sorts, many=True)
    return JsonResponse(parent_sorts_ser.data, safe=False)
Example #6
0
def get_parent_sorts(request):
    parent_sorts = Category.objects.filter(is_parent=1).only('category_name')
    parent_sorts_ser = CategorySerializers(instance=parent_sorts, many=True)
    return Response({"parent": parent_sorts_ser.data})