Addition, deletion, modification and query of Odoo

odoo built-in function definition

create function: (increase)

    @api.model
    def create(self, vals_list):
        """
        作用:记录创建方法。创建记录的orm函数。页面新建点击保存时触发调用。返回值为创建成功的记录集
        self:模型对象
        vals_list:页面数据列表,即要插入数据库中一条完整的数据列表,
        return: 返回模型对象,此处为ArticleAuthor对象,数据创建完成
        """
        article_author_obj = super(ArticleAuthor, self).create(vals_list)  
        # 调用odoo自定义的create方法创建ArticleAuthor对象
        return article_author_obj

unlink function: (delete)

    def unlink(self):
        """
        作用:删除记录
        return:删除成功则返回True,失败则返回False
        """
        res = super(ArticleAuthor, self).unlink()
        return res

write function: (change)

    def write(self, values):
        """
        作用:修改记录时使用
        values:修改页面的数据列表
        return:修改成功则返回True,失败则返回False
        """
        res = super(ArticleAuthor, self).write(values)
        return res

read, search, and search_read functions (check)

1) read function

    def read(self, fields=None, load='_classic_read'):
        """
        作用:读取一条数据
        fileds:读取条件,即读取对应的数据字段
        return: 返回读取的数据列表
        """
        res = super(ArticleAuthor, self).read(['name', 'age'])
        #  读取数据时只获得name和age字段的值
        print(res)
        return res

2) search function


Generally, the first list of search is not rewritten as domain, and limit is to output a piece of data

search([
       ('name', '=', attachment_name),
       ('res_model', '=', self.model),
       ('res_id', '=', record.id)
       ], limit=1)

3) search_read function

search_read(domain,fileds)

odoo built-in functions use

create function: (increase)

  • create(vals_list)
    such as: test .create({'name': "New Name"})
    namely: in the test object, create a new record

unlink function: (delete)

  • unlink()
    is as follows:
    test = self.env['res.users']
    test.search([('id', '=', '1')]).unlink()
    
    That is: first get the res.users table as an object, and then delete the record with id 1 in the table

write function: (change)

  • write(vals_list)
    is as follows:
    rs = demo.search([('name', '=', client_id)])
    info = {}
    info[‘id’]=1
    rs.write(info)
    
    That is: first obtain the table object that needs to be modified, and then update the data of a certain record in the table

read, search, and search_read functions: (check)

1) read function

usage:read(self,fields)

2) search function

  • Query all data in the model
    result= self.env['res.users'].search([])

  • Query specified condition data
    result.search([('id', '=', 1), ('status', '=', True)])

  • Query the first 2 pieces of data

    result.search([
          ('name', '=', attachment_name),
          ('res_model', '=', self.model),
          ('res_id', '=', record.id)
          ], limit=2)
    
  • Association query

    student = self.env['emp.student']
    teacher = self.env['emp.teacher']
    demo = teacher.search([('id', '=', student.search([('id', '=', 1)]).teacher_id.id)])
    print (demo)
    print (demo.name)
    print (demo.sex)#类似于sql中的嵌套查询
    

3) search_read function

usage:search_read(domain,fileds)

Note: If you don’t need permissions to add, delete, modify and check, add sudo() directly after the model (generally applicable to interfaces)
such as: request.env[“academy.teachers”].sudo().search([(“id ", "=", pk)])

Guess you like

Origin blog.csdn.net/weixin_44141284/article/details/128533198