当前位置:网站首页>The flask to add and delete
The flask to add and delete
2022-08-10 02:52:00 【Chen Xin ^@^】
One-to-many creation table
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'))
Many-to-many creation table
user_channel=db.Table(
'user_channel',
db.Column('user_id',db.Integer,db.ForeignKey('user_model.uid'),primary_key=True), # user_model The name of the table in the database
db.Column('channel_id',db.Integer,db.ForeignKey('channel_model.cid'),primary_key=True), # channel_model The name of the table in the database
)
#------------------------------------------------------------------------------------#
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='this guy sucks,什么都没有留下',comment='简介')
status=db.Column(db.Boolean,default=False,comment='true:冻结 false:正常')
news=db.relationship('NewModel',backref='users')
comment=db.relationship('CommentModel',backref='comment')
#================================================================#
# Create a table of channels
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.Whether the user exists in the databaseid进行查找
5.进行获取
6.没有idto get all the data
代码实现
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':'Getting a single data succeeded',
'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':'Get all data successfully',
'data':list
})
修改
思路
1.获取数据
2.获取字段
3.实例化数据
4.Determine if there is any data to be modifiedid
5.Return a response if not
6.Otherwise, proceed to modify the dataid进行修改
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')
边栏推荐
- Solve the problem of sed replacement text containing special characters such as "/" and "#"
- 【机器学习】随机森林、AdaBoost、GBDT、XGBoost从零开始理解
- 【论文粗读】(NeurIPS 2020) SwAV:对比聚类结果的无监督视觉特征学习
- 不是吧,连公司里的卷王写代码都复制粘贴,这合理?
- Golang nil的妙用
- 实操|风控模型中常用的这三种预测方法与多分类场景的实现
- unity 报错 Unsafe code may only appear if compiling with /unsafe. Enable “Allow ‘unsafe‘ code“ in Pla
- 【wpf】拖拽的简单实现
- 桌面云组件介绍与安装
- The shell specifies the parameter name to pass the parameter
猜你喜欢
随机推荐
hint: Updates were rejected because the tip of your current branch is behind hint: its remote counte
实操|风控模型中常用的这三种预测方法与多分类场景的实现
微生物是如何影响身体健康的
卷积神经网络识别验证码
Button countdown reminder
常用正则备查
首次在我们的centos上安装MySQL
网络爬虫错误
FusionCompute产品介绍
hopscotch game
按钮倒计时提醒
Screen 拆分屏幕
牛客刷题——剑指offer(第四期)
Janus实际生产案例
Teach you how to write performance test cases
Visual low-code system practice based on design draft identification
2022年8月1日-8月7日(本周10小时,合计1422小时,剩余8578小时)
使用IDEA的PUSH常见问题
[LeetCode] Find the sum of the numbers from the root node to the leaf node
Unity image使用长图后 图片很糊