博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Django2.1-mysql学习(五)
阅读量:3916 次
发布时间:2019-05-23

本文共 2713 字,大约阅读时间需要 9 分钟。

Django2.1-mysql学习(五)

今天需完成礼品的返回以及礼品的增加,在我们所设的业务场景中,仓库管理员只对礼品的基础数据负责,即礼品的上下架、礼品的热度和折扣信息不归仓库管理员处理。所以在仓库管理员添加礼品时,应注意这些字段应直接赋值。

礼品的返回:

views.gifts
method:GET
"""gifts返回仓库中的礼品信息,如果是仓库管理员,就返回自己仓库的信息,如果是普通员工,就返回所有礼品信息method: GET"""def gifts(request, employee_id):    employee = Employee.objects.get(pk=employee_id)    context = {        'error': 0    }    if(not employee) or ('EMPLOYEE_ID' not in request.session) or ('IS_LOGIN' not in request.session) \        or (request.session['EMPLOYEE_ID'] != employee_id):        context['error'] = 1        return HttpResponse(json.dumps(context), content_type="application/json")    if request.method == 'GET':    	# 如果是普通员工,返回所有礼品        if employee.order == 1:            presents = Present.objects.all()            conx = serializers.serialize("json", presents)            return HttpResponse(conx, content_type="application/json")        # 如果是仓库管理员,返回自己仓库的礼品信息        elif employee.order == 2:            depots = Depot.objects.get(manager=employee.id)            presents = Present.objects.filter(pdepot=depots)            conx = serializers.serialize("json", presents)            return HttpResponse(conx, content_type="application/json")        else:            context['error'] = 2            return HttpResponse(json.dumps(context), content_type="application")    return

添加礼品(仅仓库管理员可操作)

views.add

method:POST

"""add添加一个礼品,仓库管理员可操作,但添加时其默认状态值为0,即待审核状态method:POST"""def add(request, employee_id):    employee = Employee.objects.get(pk=employee_id)    context = {        'error': 0    }    if (not employee) or ('EMPLOYEE_ID' not in request.session) or ('IS_LOGIN' not in request.session) \        or (request.session['EMPLOYEE_ID'] != employee_id):        context['error'] = 1        return HttpResponse(json.dumps(context), content_type="application/json")    if request.method == 'POST':        pname = request.POST['name']        introduction = request.POST['introduction']        on_date = request.POST['on_date']        store_num = request.POST['store_num']        status = 0        cost = request.POST['cost']        hot = 0        off = 0        off_cost = 0        url = request.POST['url']        depot = Depot.objects.get(manager=employee)        obj = Present(name=pname, introduction=introduction, on_date=on_date, store_num=int(store_num),                      status=status, cost=float(cost), hot=int(hot), off=int(off),                      off_cost=off_cost, url=url, pdepot=depot)        obj.save()        present = Present.objects.filter(id=obj.id)        conx = serializers.serialize("json", present)        return HttpResponse(conx, content_type="application/json")    return

注:博主并没有写前端界面,只是使用postman来测试API,如果懒得写前端测试界面,可以去一起使用postman,一个非常好用的工具

源码参考:

转载地址:http://faprn.baihongyu.com/

你可能感兴趣的文章
TensorFlow 2学习和工业CV领域应用 心得分享
查看>>
程序员过关斩将--真的可以用版本号的方式来保证MQ消费消息的幂等性?
查看>>
Java面试必问JVM调优,那.NET5呢?
查看>>
把 Console 部署成 Windows 服务,四种方式总有一款适合你!
查看>>
缓存一致性和跨服务器查询的数据异构解决方案canal
查看>>
BeetleX之Websocket服务使用
查看>>
【源码】常用的人脸识别数据库以及上篇性别识别源码
查看>>
深入探究ASP.NET Core Startup的初始化
查看>>
跟我一起学Redis之Redis配置文件啃了一遍之后,从尴尬变得有底气了(总结了一张思维图)...
查看>>
一路踩坑,被迫聊聊 C# 代码调试技巧和远程调试
查看>>
IdentityServer4系列 | 资源密码凭证模式
查看>>
TIOBE 11 月榜单:Python 挤掉 Java,Java的下跌趋势确立了?
查看>>
C#实现观察者模式
查看>>
使用Azure静态Web应用部署Blazor Webassembly应用
查看>>
Win10 Terminal + WSL 2 安装配置指南,精致开发体验
查看>>
Xamarin 从零开始部署 iOS 上的 Walterlv.CloudKeyboard 应用
查看>>
【招聘(西安)】深圳市中兴云服务有限公司.NET工程师
查看>>
注意.NET Core进行请求转发问题
查看>>
别“躺”着了,赶紧把「复盘」做起来
查看>>
真正拖垮你的,是沉没成本
查看>>