当前位置:网站首页>flask增删改查
flask增删改查
2022-08-10 01:34:00 【晨馨^@^】
一对多创表
class User(db.Model):
uid=db.Column(db.Integer,primary_key=True,autoincrement=True,comment='用户ID')
mobile=db.Column(db.String(11),comment='手机号')
password=db.Column(db.String(125),comment='密码')
username=db.Column(db.String(156),comment='用户名')
add=db.relationship('Address',backref='address')
#----------------------------------------------------------------------------#
class Address(db.Model):
aid = db.Column(db.Integer, primary_key=True, autoincrement=True, comment='用户ID')
add=db.Column(db.String(256),comment='地址')
add_new=db.Column(db.String(256),comment='详细地址')
name=db.Column(db.String(256),comment='名字')
mobile=db.Column(db.String(11),comment='电话')
add_id=db.Column(db.Integer,db.ForeignKey('user.uid'))
多对多创表
user_channel=db.Table(
'user_channel',
db.Column('user_id',db.Integer,db.ForeignKey('user_model.uid'),primary_key=True), # user_model 数据库中的表的名字
db.Column('channel_id',db.Integer,db.ForeignKey('channel_model.cid'),primary_key=True), # channel_model 数据库中的表的名字
)
#------------------------------------------------------------------------------------#
class UserModel(db.Model):
uid=db.Column(db.Integer,primary_key=True,autoincrement=True,comment='用户ID')
mobile=db.Column(db.String(11),comment='用户手机号')
username=db.Column(db.String(32),comment='昵称',default='')
img=db.Column(db.String(300),comment='头像',default='')
reg_time=db.Column(db.DateTime,default=datetime.now,comment='注册时间')
intro=db.Column(db.String(256),default='这个人很烂,什么都没有留下',comment='简介')
status=db.Column(db.Boolean,default=False,comment='true:冻结 false:正常')
news=db.relationship('NewModel',backref='users')
comment=db.relationship('CommentModel',backref='comment')
#================================================================#
# 创建频道的表
class ChannelModel(db.Model):
cid=db.Column(db.Integer,primary_key=True,autoincrement=True,comment='id')
name=db.Column(db.String(16),comment='频道名')
num=db.Column(db.Integer,comment='数字越大,排名越靠前')
user=db.relationship('UserModel',secondary=user_channel,backref=db.backref('channels'))
news=db.relationship('NewModel',backref='channels')
增加
思路
1.获取数据
2.填入数据
3.实例化
4.对数据进行添加
5.进行提交
6.返回响应
代码实现
def post(self):
req=reqparse.RequestParser()
req.add_argument('title')
req.add_argument('people')
req.add_argument('shebei')
req.add_argument('hui_id',required=True)
args=req.parse_args()
db.session.add(HuiYi(
title=args['title'],
people=args['people'],
shebei=args['shebei'],
hui_id=args['hui_id']
))
db.session.commit()
return jsonify({
'code':200,
'msg':'添加数据成功'
})
查找
思路
1.校验数据 获取用户的id
2.获取id
3.校验数据
4.对数据库中是否存在用户id进行查找
5.进行获取
6.没有id的话进行获取所有的数据
代码实现
req=reqparse.RequestParser()
req.add_argument('hui_id')
args=req.parse_args()
hui=HuiYi.query.filter(HuiYi.hui_id==args['hui_id']).first()
list=[]
if hui:
list.append({
'hid':hui.hid,
'hui_id': hui.hui_id,
"title": hui.title,
"people":hui.people,
'shebei':hui.shebei
})
return jsonify({
'code':200,
'msg':'获取单个数据成功',
'data':list
})
all=HuiYi.query.all()
for i in all:
list.append({
'hid': i.hid,
'hui_id': i.hui_id,
"title": i.title,
"people": i.people,
'shebei': i.shebei
})
return jsonify({
'code':200,
'msg':'获取全部数据成功',
'data':list
})
修改
思路
1.获取数据
2.获取字段
3.实例化数据
4.判断是否存在要修改数据的id
5.如果没有则返回响应
6.否则进行对要修改数据的id进行修改
7.成功返回响应
def put(self):
req=reqparse.RequestParser()
req.add_argument('hid',required=True)
req.add_argument('title')
req.add_argument('people')
req.add_argument('shebei')
args=req.parse_args()
user_info=HuiYi.query.get(args['hid'])
if not user_info:
return jsonify({
'code':200,
'msg':'会议不存在'
})
user_info.title=args['title']
user_info.people=args['people']
user_info.shebei=args['shebei']
db.session.commit()
return jsonify({
'code':200,
'msg':'数据修改成功'
})
删除
思路
1.获取数据
2.获取必要的id
3.进行实例化
4.直接过滤出id对数据进行删除
5.进行提交‘’
6.返回响应
def delete(self):
req=reqparse.RequestParser()
req.add_argument('hid',required=True)
args=req.parse_args()
HuiYi.query.filter(HuiYi.hid==args['hid']).delete()
db.session.commit()
return jsonify({
'code':200,
'msg':'删除成功'
})
api.add_resource(YiLogin,'/yilogin')
边栏推荐
猜你喜欢

c# 解决CS8602告警 解引用可能出现空引用

Janus actual production case

Not, even the volume of the king to write code in the company are copying and pasting it reasonable?

微透镜阵列后光传播的研究

Process management and task management

牛客刷题——剑指offer(第四期)

UI遍历的初步尝试

Under pressure, there must be cowards

color socks problem

Shader Graph学习各种特效案例
随机推荐
你有对象类,我有结构体,Go lang1.18入门精炼教程,由白丁入鸿儒,go lang结构体(struct)的使用EP06
C# 四舍五入 MidpointRounding.AwayFromZero
具有多孔光纤的偏振分束器
hint: Updates were rejected because the tip of your current branch is behind hint: its remote counte
LeetCode 每日一题——1413. 逐步求和得到正数的最小值
gbase 8a数据库如何查看数据或数据文件是否正常?
[QNX Hypervisor 2.2用户手册]10.14 smmu
Initial attempt at UI traversal
通关剑指 Offer——剑指 Offer II 012. 左右两边子数组的和相等
2022年8月1日-8月7日(本周10小时,合计1422小时,剩余8578小时)
Open3D 网格均匀采样
Unity reports Unsafe code may only appear if compiling with /unsafe. Enable “Allow ‘unsafe’ code” in Pla
OpenCV图像处理学习一,加载显示修改保存图像相关函数
[语法糖] 关于类别字符串到类别数字id的映射
控制台中查看莫格命令的详细信息
2022金九银十工作潮,怎么样才能成功跳槽面试拿到高薪呢?
Janus actual production case
idea 删除文件空行
夏克-哈特曼波前传感器
《GB39732-2020》PDF download