Day7 Let's ODOO: Model(4) ORM API

本章节介绍ORM对资料库的CURD方法,我们以上个章节介绍的student model 为例。

# -*- coding: utf-8 -*-

from odoo import api, models, fields
from odoo.exceptions import ValidationError

class ResStudent(models.Model):
	_name = 'res.student'
    _inherit = 'res.partner'
    _description = 'Student'

    nickname = fields.Char(string='绰号')
    math_score = fields.Float(string='数学成绩')
    chinese_score = fields.Float(string='国文成绩')
    avg_score = fields.Float(string='学期平均', compute='_compute_score')
    birthday = fields.Date(string='生日', required=True)
    school_id = fields.Many2one('res.company', string='所属学校')
    school_city = fields.Char(string='所在城市', related='school_id.city')
    senior_id = fields.Many2one('res.student', string='直属学长姐')
    junior_ids = fields.One2many('res.student', 'senior_id', string='直属学弟妹')
    teacher_ids = fields.Many2many('res.partner', string='指导老师', domain=[('is_company', '!=', True)])
    gender = fields.Selection([("male", "男"), ("female", "女"), ("other", "其他")], string='性别')
    is_leadership = fields.Boolean(default=False)
    is_active = fields.Boolean(default=True)

CREATE

create()

新增方法很直观,直接对新增的栏位create即可。

env['res.student'].create({'name': '王大明', 'nickname': '小明', 'birthday': '2000-01-01'})

copy()

当一个资料里可能含有许多field,我们想要其中一份一样的资料,为了方便就可以使用copy。

env['res.student'].search([('name', '=', '王大明')]).copy()

UPDATE

write()

将当前的recordset根据指定的栏位修改

env['res.student'].search([('name', '=', '王大明')]).write({'nickname': '大明'})

要注意的是当需要新增或修改One2manyMany2many field时,ODOO有一套自己的写法,这边饮用ODOO的Document:

  • (0, 0, values) adds a new record created from the provided value dict.
  • (1, id, values)updates an existing record of id id with the values in values. Can not be used in create()
  • (2, id, 0) removes the record of id id from the set, then deletes it (from the database). Can not be used in create()
  • (3, id, 0) removes the record of id id from the set, but does not delete it. Can not be used in create()
  • (4, id, 0) adds an existing record of id id to the set.
  • (5, 0, 0) removes all records from the set, equivalent to using the command 3 on every record explicitly. Can not be used in create()
  • (6, 0, ids) replaces all existing records in the set by the ids list, equivalent to using the command 5 followed by a command 4 for each id in ids.

举个例子,当我想为王大明增加一位还没建资料的学弟妹,并将一位id=2的同学因为他作弊,修改他的成绩为零分

first_val = {'name': '林小美', 'birthday': '2001-03-03'}
second_val = {'math_score': 0}
env['res.student'].search([('name', '=', '王大明')]).write('junior_ids': [(0, 0, first_val), (1, 2, second_val)])

READ

search()

ODOO中的domain可以想成是query条件,search可以寻找所需要的资料集。

env['res.student'].search([('is_active', '=', True )], limit=10)

我们寻找还在学的学生10位,[('is_active', '=', True )] 为domain的写法,前者为参数,中间为运算元(operator),後面为值。

search_count()

search() 方法相同,只是最後只会return资料个数。

browse()

根据给予的ids阵列,return 资料。

env['res.student'].browse([2,3,6])

exists()

检查recordset是否存在

DELETE

unlink()

删除资料集

env['res.student'].search([('name', '=', '王大明')]).unlink()

Model所有的介绍就到这边,笔者介绍的大部分都是自己用过或是比较常看到的,更多相关的参数或方法可以参考ODOO Document,明天将会介绍到View的章节。


<<:  简单说回归 | ML#Day14

>>:  Day 22 - Rancher Fleet 架构介绍

Spring Framework X Kotlin Day 25 Behavior Driven Development

GitHub Repo https://github.com/b2etw/Spring-Kotlin...

实战练习 - 使用 RxJS 实作「自动完成 / 搜寻 / 排序 / 分页」功能

今天我们用实际的例子来练习各种 RxJS operators 的组合运用!在一般的应用程序里面,资料...

[Android Studio菜鸟的学习分享]完赛结语

三十天说常不常说短不短, 今天是最後一天了, 回顾这三十天真的觉得不可思议我居然能坚持到现在, 这次...

Day 3. Pre-Start × WYSIWYG × contenteditable

诚如上一篇结尾所说,我们今天要把目光聚焦在浏览器提供的 contenteditable 属性以及 ...

CSS微动画 - Transform不一定是位移的最佳选择

Q: 效能跟效果之间怎麽取舍? A: 如果效果不复杂,用一些渲染成本比较高的写法也无妨 新属性搭配...