2014-02-28 62 views
1

我正在使用OpenERP v7.0開發OpenERP自定義模塊工作單。我一直試圖從父窗體傳遞一個值到一個子記錄,以便通過many2one字段準備子記錄並將其鏈接到其父項。OpenERP將父值到子值中的值傳遞給父項創建記錄

我已經覆蓋創建方法,所以在父母work_order.contract「:

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'中使用寫入方法:

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,因爲它尚不存在(在創建過程中)。

如果我所做的是錯誤的,請告訴我另一種準備孩子記錄的方法,以瞭解其父記錄。謝謝=)

回答

3

在父類:

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在子記錄可用。

+0

所以我只是錯過添加該字典作爲一個新的變量,並將其傳遞給子記錄?該死的... 我會試試這個,謝謝很多兄弟! – Idzham

+0

謝謝,它正確地傳遞給子記錄,但它似乎只能創建一個單獨的記錄,否則它是「完整性錯誤,work_order_code必須是唯一的」。如果vals.get('work_order_code','/')=='/':「因爲只能識別字符」/「,所以通過在def記錄中的def create()中添加else來解決。否則,它將使用與因此之前完整性錯誤。 我多麼傻,沒有考慮設置一個臨時變量來傳遞字典中的一個值「self.pool.get('work_order.work_order')。create(cr,uid,child_id,context = context) 再次感謝分享=) – Idzham

相關問題