插入Many2many Odoo(以前的OpenERP)

发布于 2025-01-07 19:42:48 字数 102 浏览 2 评论 0原文

我正在尝试将值插入 Odoo(以前的 OpenERP)中的 Many2manyOne2many 关系表字段。您知道如何做到这一点吗?

I'm trying to insert values into a Many2many or One2many relation table field in Odoo (former OpenERP). Do you have any idea how to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

烛影斜 2025-01-14 19:42:48

以下是 stock 模块<的示例/a>:

invoice_line_id = invoice_line_obj.create(cursor, user, {
    'name': name,
    'origin': origin,
    'invoice_id': invoice_id,
    'uos_id': uos_id,
    'product_id': move_line.product_id.id,
    'account_id': account_id,
    'price_unit': price_unit,
    'discount': discount,
    'quantity': move_line.product_uos_qty or move_line.product_qty,
    'invoice_line_tax_id': [(6, 0, tax_ids)],
    'account_analytic_id': account_analytic_id,
    }, context=context)
self._invoice_line_hook(cursor, user, move_line, invoice_line_id)

invoice_line_tax_id字段是多对多关系,(6, 0,tax_ids)表示用 tax_ids 中的记录替换任何现有记录。因为您正在调用 create(),所以没有任何内容可以替换。

完整的选项列表位于文档中对于 osv 类

对于多对多字段,需要一个元组列表。这是接受的元组列表,具有相应的语义

(0, 0, {values }) 链接到需要使用给定值字典创建的新记录

(1, ID, { value }) 使用 id = ID 更新链接记录(在其上写入

(2, ID) 删除并删除 id = ID 的链接记录(在 ID 上调用 unlink,这将完全删除该对象及其链接)

(3, ID)切断id=ID的链接记录的链接(删除两个对象之间的关系但不删除目标对象本身)

(4, ID) 链接到 id = ID 的现有记录(添加关系)

(5) 取消所有链接(例如对所有链接记录使用 (3,ID))

(6, 0, [IDs]) 替换链接 ID 列表(例如对 ID 列表中的每个 ID 使用 (5),然后使用 (4,ID))

Here's an example from the stock module:

invoice_line_id = invoice_line_obj.create(cursor, user, {
    'name': name,
    'origin': origin,
    'invoice_id': invoice_id,
    'uos_id': uos_id,
    'product_id': move_line.product_id.id,
    'account_id': account_id,
    'price_unit': price_unit,
    'discount': discount,
    'quantity': move_line.product_uos_qty or move_line.product_qty,
    'invoice_line_tax_id': [(6, 0, tax_ids)],
    'account_analytic_id': account_analytic_id,
    }, context=context)
self._invoice_line_hook(cursor, user, move_line, invoice_line_id)

The invoice_line_tax_id field is a many-to-many relationship, and the (6, 0, tax_ids) means to replace any existing records with those in tax_ids. Because you're calling create(), there's nothing to replace.

A full list of options is in the documentation for the osv class.

For a many2many field, a list of tuples is expected. Here is the list of tuple that are accepted, with the corresponding semantics

(0, 0, { values }) link to a new record that needs to be created with the given values dictionary

(1, ID, { values }) update the linked record with id = ID (write values on it)

(2, ID) remove and delete the linked record with id = ID (calls unlink on ID, that will delete the object completely, and the link to it as well)

(3, ID) cut the link to the linked record with id = ID (delete the relationship between the two objects but does not delete the target object itself)

(4, ID) link to existing record with id = ID (adds a relationship)

(5) unlink all (like using (3,ID) for all linked records)

(6, 0, [IDs]) replace the list of linked IDs (like using (5) then (4,ID) for each ID in the list of IDs)

一江春梦 2025-01-14 19:42:48
def list_customers(self, cr, uid, ids, context):
    sale_obj = self.pool.get('sale.order')
    for sale in self.browse(cr, uid, ids, context):
        sale_ids = sale_obj.search(cr, uid, [('div_code_id','=',sale.div_code_id.id),('project_user','=',sale.project_id.id),('tower_id','=',sale.tower_id.id)])
        ids_cus = []
        for cus in sale_obj.browse(cr, uid, sale_ids, context):
            if cus.partner_id.id not in ids_cus:
                ids_cus.append(cus.partner_id.id)
        self.write(cr, uid, ids, {'state_readonly':'listed','customer_ids': [(6, 0, ids_cus)]})
    return True

您可以将值插入到OpenERP中的多对多关系表中,请看上面的示例

def list_customers(self, cr, uid, ids, context):
    sale_obj = self.pool.get('sale.order')
    for sale in self.browse(cr, uid, ids, context):
        sale_ids = sale_obj.search(cr, uid, [('div_code_id','=',sale.div_code_id.id),('project_user','=',sale.project_id.id),('tower_id','=',sale.tower_id.id)])
        ids_cus = []
        for cus in sale_obj.browse(cr, uid, sale_ids, context):
            if cus.partner_id.id not in ids_cus:
                ids_cus.append(cus.partner_id.id)
        self.write(cr, uid, ids, {'state_readonly':'listed','customer_ids': [(6, 0, ids_cus)]})
    return True

You can insert values into a many-to-many relation table in OpenERP, please look at above example

空心↖ 2025-01-14 19:42:48

当我们创建 Many2many 字段时,我们使用了以下语法:

'field_name':fields.many2many('Module_name','relation_name','self_id','module_name_id','string', 

现在您需要通过执行如下查询来插入此关系:

 $ cr.execute('insert into relation_name (self_id,module_name_id) values(%s,%s)',(first_value,second_value)

When we create the many2many field then we used this syntax:

'field_name':fields.many2many('Module_name','relation_name','self_id','module_name_id','string', 

Now u need to insert into this relation by execute queries like:

 $ cr.execute('insert into relation_name (self_id,module_name_id) values(%s,%s)',(first_value,second_value)
泡沫很甜 2025-01-14 19:42:48

只需将您的 Many2many 字段放入视图(xml 文件)中,运行模块后,您可以看到 Many2many 字段,以便在 GUI 中插入记录

Just put your many2many field in view (xml file) and after running your module you can see many2many field to insert records in your gui

怪我入戏太深 2025-01-14 19:42:48

对于在 Many2many 字段中添加数据:

self.m2m_field = [(4, self.project_id.id)]

对于在 one2many 字段中添加数据:

#Create list

data_list = []

#Adding data in list:

data_list.append((0, 0, {
    'user_id': self.user_id.id,
    'note': self.note,
    'create_date': self.create_date
}))

#Assign list to one2many field:

self.log_id = data_list

For adding data in Many2many field:

self.m2m_field = [(4, self.project_id.id)]

For adding data in one2many field:

#Create list

data_list = []

#Adding data in list:

data_list.append((0, 0, {
    'user_id': self.user_id.id,
    'note': self.note,
    'create_date': self.create_date
}))

#Assign list to one2many field:

self.log_id = data_list
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文