2017-10-18 62 views
2

在pos.quotation模型中,我們有狀態。所以我的目標是當狀態發生變化時,我希望shoes.order中的布爾字段名爲「handed」,並將變爲true。我知道如何做到這一點,如果我會在一個模型中做到這一點,但當我需要改變其他領域的領域時,我會奮鬥。從其他模型中更改字段值

class pos_quotation(models.Model): 
    _inherit = "pos.quotation" 

    @api.onchange('state') 
    def handed(self): 
     shoes = self.env['shoes.order'] 
     for rec in self: 
      if self.state == "delivery_success": 
       rec.shoes.handed = True 

回答

1

電平變化中的自包含虛擬對象當你改變值數據庫層上 沒有發生。 (相對於依賴計算域)

但是原始值在self._origin中傳遞。

@api.onchange('state') 
def handed(self): 
if self.state == "delivery_success": 
       # first try this if it work 
       self._origin.shoes.handed = True 

       # if not working then you need to fetch the recorod 
       # from the database first. 
       shoes = self.env['shoes.order'].search[('id', '=', self.shoes.id)] 

       shoes.handed = True 

但在onchange事件這樣做可能會導致一些問題,成像用戶具有 改變了主意,點擊取消(更改discared),但shoes.handed是 人準備COMMITED數據庫。

我對你的支持是使用相關領域。

class pos_quotation(models.Model): 
    _inherit = "pos.quotation" 

    # i'm assuming that your m2o field is shoes 
    # don't make readonly because you need to save it's changed value 
    # when you hit save. 
    handed = fields.Boolean(related="shoes.handed") 

    @api.onchange('state') 
    def handed(self): 
     if self.state == "delivery_success": 
       self.handed = True 

不要忘了這個字段添加到表單視圖,並確保它是無形的 所以用戶不更新值手動

<field name="handed" invisible="1"/> 

希望你有這個想法。

相關問題