Flask单表增删改查

模型类

from flask_sqlalchemy import SQLAlchemy

db = SQLAlchemy()


class CategoryModel(db.Model):
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    name = db.Column(db.String(100), comment="分类名")
    icon = db.Column(db.String(100), comment="图标")

from flask import Blueprint, jsonify, request
from flask_restful import reqparse, Resource, Api
from model.model import GoodsModel, CategoryModel, db

good_bp = Blueprint("good_bp", __name__, url_prefix="/good")

api = Api(good_bp)


class CategoryView(Resource):
    def get(self):
        cate = CategoryModel.query.all()
        temp = []
        for i in cate:
            temp_dict = {
                "id": i.id,
                "name": i.name,
                "icon": i.icon
            }
            temp.append(temp_dict)
        return jsonify({
            "code": 200,
            "msg": "数据展示成功",
            "data": temp
        })


class OneCate(Resource):
    def get(self, id):
        i = CategoryModel.query.get(id)
        if not i:
            return jsonify({
                "code": 400,
                "msg": "标题不存在"
            })

        temp = {
            "id": i.id,
            "name": i.name,
            "icon": i.icon
        }
        return jsonify({
            "code": 200,
            "msg": "数据展示成功",
            "data": temp
        })



api.add_resource(CategoryView, "/cate")
api.add_resource(OneCate, "/cate/<int:id>")

增加

class CategoryView(Resource):    
    def post(self):
        req = reqparse.RequestParser()
        req.add_argument("name")
        req.add_argument("icon")
        args = req.parse_args()
        name = args["name"]
        icon = args["icon"]
        if not name and not icon:
            return jsonify({
                "code": 400,
                "msg": "数据不能为空"
            })
        cate = CategoryModel(name=name, icon=icon)
        db.session.add(cate)
        db.session.commit()

        return jsonify({
            "code": 200,
            "msg": "添加成功"
        })

删除

class OneCate(Resource):
    def delete(self, id):
        cate = CategoryModel.query.get("id")

        if not cate:
            return jsonify({
                "code": 400,
                "msg": "标题不存在"
            })

        CategoryModel.query.filter(CategoryModel.id == id).delete()
        return jsonify({
            "code": 200,
            "msg": "删除成功"
        })

修改

class OneCate(Resource):
    def put(self, id):
        req = reqparse.RequestParser()
        req.add_argument("name")
        req.add_argument("icon")
        args = req.parse_args()
        name = args["name"]
        icon = args["icon"]
        cate = CategoryModel.query.get(id)

        if not cate:
            return jsonify({
                "code": 400,
                "msg": "标题不存在"
            })

        if name and name != cate.name:
            cate.name = name
        if icon and icon != cate.icon:
            cate.icon = icon

        db.session.commit()

        return jsonify({
            "code": 200,
            "msg": "更新成功"
        })

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值