odoo16--接口编写小白教程--02
在接口文件中实现增、删、改、查操作
目标:实现删除一只猫猫信息
- 在页面中可以看到当前已有四只猫猫信息
首先如果需要删除某一只猫猫的信息,应当获取到这一只猫猫的id,则我们可以通过查看后台form页面,也可以从接口查询中得知。
这里我们尝试删除“测试猫猫”,后台form页面显示其id为“5”
- 在controller层的AllData类中继续写接口
# 删除某一猫猫数据
'''
uid 猫猫id
'''
@http.route('/unlinkCatData', type='json', auth="none", cors="*", csrf=False)
def unlinkCatData(self, **kw):
cat_obj = request.env['my.cats'].sudo() #获取猫猫模块的所有数据表集
cat_id = cat_obj.search([('id','=',int(kw['uid']))]) #定位到id等于传入的uid猫猫数据表
if not cat_id:
return{
"code": 1,
"data": "猫猫不存在"
}
cat_id.unlink() #将该id的猫猫数据表删除,unlink()为odoo的数据删除操作方法
return{
"code": 1, #这里的code表示返回给前端一个信息,表示接口调用成功
"data": "删除成功"
}
- 保存代码重启odoo,在postman进行删除操作
- 在odoo社区页面查看,仅三条猫猫数据
目标:实现新增一只猫猫
- 在controller层的AllData类中继续写接口
#增加一只猫猫信息
'''
name 猫猫名称
age 年龄
doing_date 入店时间
price 定价
description 内容简介
cat_type 品种类型
borrowed 是否被收养
'''
@http.route('/createOneCatData', type='json', auth="none", cors="*", csrf=False)
def createOneCatData(self, **kw):
cat_obj = request.env['my.cats'].sudo()
cat_id = cat_obj.search([('name','=',kw['name'])]) #定位到与输入参数name一致的猫猫
#如果存在,则提示猫猫不能重名,并终止此接口函数
if cat_id:
return "猫猫不能重名哦!"
#否则可以新增猫猫信息
else:
cat_obj.create({ #create()是odoo数据表新增信息的方法,以字典的数据类型传入数据表
"name":kw.get('name'), #关键字“name”,“age”等必须是my.cats模块中的字段
"age":kw.get('age'), #kw.get获取前端传入关键字为“age”的数据
"doing_date":kw.get('doing_date'),
"price":kw.get('price'),
"description":kw.get('description'),
"cat_type":kw.get('cat_type'),
"borrowed":kw.get('borrowed')
})
return{
'code':1,
'data':"新增猫猫成功"
}
- 在postman测试,如果新增的猫猫姓名与后台数据有重合,则提示:
这里输入的参数关键字name的值与后台数据库的name值存在重复,则返回前端提示“猫猫不能重名”
-
在postman测试新增猫猫数据
-
传入的参数数据类型:根据model中定义的字段类型进行传参
-
my.cats模块,model字段定义:
class MyCatHomePage(models.Model):
_name = "my.cats"
_description = 'My Cats Homepage'
name = fields.Char(string='猫猫名称',required=True) #require表示必填
code = fields.Char(string='猫猫编号',copy=False,help='管理编号,用于快速定位猫猫')
age = fields.Integer(string='年龄') #整数类型字段
doing_date = fields.Date(string='入店时间') #日期类型字段
# digits是一个元组,前面的数表示该数字的总位数,后面的数字表示小数点的精度
price = fields.Float(string='定价',digits=(7,2)) #浮点数类型字段
description = fields.Text(string='内容简介',help="""向借购买者描述本主子内容""")
# index:设置为true则会在对应的数据库字段上添加索引,提升该字段进行查询的效率
#selection 选择字段,这里默认为普通
cat_type = fields.Selection([('common','普通'),('hardcover','纯种')],string='品种类型',index=True,default='common')
# 布尔类型数据字段
borrowed = fields.Boolean(string='是否被收养',default=False)
对照上面的model字段,可以知道我们需要传入的参数应该规定为什么数据类型:
name:字符串
age:整型
doing_date:日期字符串
description:字符串
cat_type:选择类型,这里需要传入你selection元组中的第一个参数
borrowed:布尔类型,这里需要传入True 或者 False
- 在odoo后台页面查看是否已经有此数据
可以看到传入的参数都已成功写入数据库中
目标:实现修改某一只猫猫的信息
-
当我们需要拿到指定的某一个数据表时,应该要先想到取其对应的唯一id
-
我们在controller接口层中继续编写接口代码
#修改某一只猫猫信息
#这里传入的参数需要在接口文档中规定是否必传,例如猫猫的id是必传的,用于定位到该猫猫的信息
#而其他参数则是用于修改数据的,可以是不必传,即不传入则不进行修改
'''
uid 猫猫id
name 姓名
age 年龄
doing_date 入店时间
price 定价
description 内容简介
cat_type 品种类型
borrowed 是否被收养
'''
@http.route('/writeOneCatData', type='json', auth="none", cors="*", csrf=False)
def writeOneCatData(self, **kw):
cat_obj = request.env['my.cats'].sudo()
cat_id = cat_obj.search([('id','=',int(kw['uid']))])
cat_id.write({ #write()是odoo框架中修改数据表的方法
"name":kw.get('name',cat_id.name), #后面这个cat_id.name,如果未传入参数name的值,则沿用原有数据中的数据
"age":kw.get('age',cat_id.age),
"doing_date":kw.get('doing_date',cat_id.doing_date),
"price":kw.get('price',cat_id.price),
"description":kw.get('description',cat_id.description),
"cat_type":kw.get('cat_type',cat_id.cat_type),
"borrowed":kw.get('borrowed',cat_id.borrowed)
})
return{
'code':1,
'data':"修改成功"
}
- 在postman中进行测试,我这里只对修改了名字,其余信息没有进行修改
- 在odoo后台页面中查看是否已做变更
到这里我们已经完成了接口的四大基本操作:增删改查;