当前位置:网站首页>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')
边栏推荐
猜你喜欢

【web渗透】SSRF漏洞超详细讲解

idea 删除文件空行

mysql -sql编程

hopscotch game

基于FTP协议实现文件上传与下载

OptiFDTD应用:纳米盘型谐振腔等离子体波导滤波器

按钮倒计时提醒

Chip Information|Semiconductor revenue growth expected to slow to 7%, Bluetooth chip demand still growing steadily

unity 报错 Unsafe code may only appear if compiling with /unsafe. Enable “Allow ‘unsafe‘ code“ in Pla

Unity3D创建道路插件EasyRoads的使用
随机推荐
[Turn] Typora_Markdown_ picture title (caption)
Under pressure, there must be cowards
华为HCIE云计算之FC添加ipsan数据存储
【内存管理概述 Objective-C语言】
mysql -sql编程
hint: Updates were rejected because the tip of your current branch is behind hint: its remote counte
Summary of Web Performance Testing Models
Research on Ethernet PHY Chip LAN8720A Chip
FusionConpute虚拟机的发放与管理
shell指定参数名传参
SonarQube升级记录:7.8->7.9->8.9
Experimental support for decorators may change in future releases.Set the "experimentalDecorators" option in "tsconfig" or "jsconfig" to remove this warning
Sikuli's Automated Testing Technology Based on Pattern Recognition
罗彻斯特大学 | 现在是什么序列?蛋白质序列的贝叶斯优化的预训练集成
Open3D 中点细分(网格细分)
Sikuli 基于图形识别的自动化测试技术
UXDB现在支持函数索引吗?
实操|风控模型中常用的这三种预测方法与多分类场景的实现
在蓝图中给组件动态加子Actor组件
多线程之享元模式和final原理