【问题标题】:OpenERP passing values from parent to child record in creating a record in parentOpenERP在父记录中将值从父记录传递到子记录
【发布时间】:2014-02-28 08:09:25
【问题描述】:

我正在使用 OpenERP v7.0 处理 OpenERP 自定义模块工作订单。我一直在尝试将值从父表单传递给子记录,以便准备子记录并通过 many2one 字段链接到其父记录。

我已经在父 'work_order.contract' 中重写了 create 方法:

def create(self, cr, uid, vals, context=None):
    if vals.get('contracts_code','/')=='/':
        vals['contracts_code'] = self.pool.get('ir.sequence').get(cr, uid, 'work_order.contracts') or '/'
    order = super(contracts, self).create(cr, uid, vals, context=context)

    """Creating a contract should be able to create at least one or several
    work order records so as to reflect accordingly, but it doesn't link to
    the correct contract record that create it.
    i.e. contract 3 creates 4 workorders"""

    vals['contracts_code'] = order
    for count in range(0, vals['work_orders']):
        self.pool.get('work_order.work_order').create(cr, uid, {'value' : {'contracts_code': order}}, context)
    return order

但是这个覆盖方法有点像:

Create (parent) --------Create (child)
    |    ^                |
    |    |                |
    |    |                v
    |    |______________Write (child)
    v
Write (parent)

因此不会将孩子链接到其父母,因为父母的 id 仍然存在......

我尝试在父 'work_order.contract' 中使用 write 方法:

def write(self, cr, uid, ids, vals, context=None):
    res = super(contracts, self).write(cr, uid, ids, vals, context=context)

    """if there's more than 1 work order in a contract, it should be able
    to create the work orders accordingly linked to that contract"""

    for x in range(0, vals['work_orders']):
        self.pool.get('work_order.work_order').create(cr, uid, {'value' : {'contracts_code': vals['contracts_code']}}, context)
    return res

这个覆盖方法看起来像:

Create (parent)
    |
    v
Write (parent) --------> Create (child)
           ^                 |
           |_____________Write (child)

但不知何故,需要传递的值在调用孩子创建记录的过程中不知何故消失了。

在提出这个问题时,只需意识到传递的数据是 char 而不是像 id 这样的整数,但是我不确定如何传递正确的 id,因为它还不存在(在创建过程中)。

如果我做错了,请告诉我另一种准备子记录的方法,以便了解其父记录。谢谢=)

【问题讨论】:

    标签: python postgresql openerp


    【解决方案1】:

    在父类中:

    def create(self, cr, uid, values, context=None):
        .
        .
        # prep everything for parent record
        .
        .
        new_id = super(parent_class, self).create(cr, uid, values, context=context)
        # at this point, you have the id of the new record
        .
        # prep whatever you need for child record(s)
        .
        child_values = {'parent_id':new_id, ... }
        child_class.create(cr, uid, child_values, context=context)
    

    这就是您如何在子记录中获得新的父 ID。

    【讨论】:

    • 所以我只是错过了将该字典添加为新变量并将其传递给子记录?该死的......我会试试这个,非常感谢兄弟!
    • 谢谢,它正确传递给子记录,但它似乎只能创建一条记录,否则它是“完整性错误,work_order_code 必须是唯一的”。通过在子记录“if vals.get('work_order_code','/')=='/':”中的 def create() 中添加 else 来解决,因为它只识别字符“/”,否则它将使用与因此之前的完整性错误。多么愚蠢的我没有考虑设置一个临时变量来传递字典中的值“self.pool.get('work_order.work_order').create(cr, uid, child_id, context=context) 再次感谢分享=)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多