2016-09-18 33 views
2

假設你正在設置一個名爲「動物類型」的字段,並且有一個依賴於這個名爲「最喜歡的玩具」的字段。如果「動物類型」是一隻狗,我想將「最喜歡的玩具」的域設置爲('isdogtoy','=',True)。如果它是一隻貓,那麼也許我們將它設置爲假或其他條件。Odoo v9 - 在編輯表單時設置一個動態域名(不會創建)

通常要設置動態域名,您可以使用onchange,然後爲域設置域名。

但是,有些情況下沒有任何變化。例如,如果您編輯現有記錄,則不一定會調用onchange。如果我從不改變「動物類型」,那麼我的域名「最喜歡的玩具」永遠不會通過我的onchange方法設置。

我不知道我們如何在Odoo中動態地做到這一點。看起來似乎應該有一種方式,但我無法找到任何這方面的信息。

+0

我真的不明白你的意思,如果字段'y'取決於字段'x',如果x不變,字段'y'中的任何變化如何被觸發? *動態*(改變的東西)域必須基於變化的東西。我懷疑你想要一個默認域名,但直到你澄清之前我永遠都不會知道。 – danidee

+0

好吧,說白了,我有一個改變,就是我在問題中所描述的。當我創造新紀錄時它效果很好。但是,在保存記錄並重新編輯之後,該域將重置爲默認值。 從用戶的角度來看,這確實沒有任何意義。當他們創造記錄時,他們給「最喜歡的玩具」的選擇是他們選擇的動物所特有的。現在他們去改變「最喜歡的玩具」,但Odoo給他們提供了一個不同的選擇領域。他們應該看到他們最後一次的選擇。 – Nross2781

+0

好吧,所以我有想做一個計算字段,並用它來幫助設置域中的變量....但我不能讓我的生活得到計算字段存儲在數據庫中。即使store = True,值也不會被保存。 我發誓,更新的API是真氣。我看到所有這些適用於v7的解決方案,但在v9中沒有辦法做同樣的事情。 – Nross2781

回答

1

這是最好的,我可以拿出來,使用計算字段。下面是從我的代碼

在我的XML的示例解決方案,

<field name="uom_id" position="replace"> 
     <!-- The category_id.name is really only used to filter when islocaluom=True. The result is that if a uom_class is used, only uom's from that class can be selected. Otherwise, the default uom's are present --> 

     <field name="uom_id" groups="product.group_uom" domain="['&amp;',('islocaluom','=',calcislocaluom),'|',('islocaluom','=',False),('category_id','=',calccatidname)]" options="{'no_create' : True},{'no_create_edit' : True}" /> 
    </field> 

現在我剛剛創建採用存儲=真有些計算字段,然後將它們的計算功能。

class ProductTemplate(models.Model): 
    _inherit = 'product.template' 
    #This field will let us choose if we are using per product uom on the product 
    uom_class = fields.Many2one('productuom.class', 'Per Product UOM Conversion Class', ondelete='restrict',required=False, help="Unit of Measure class for Per Product UOM") 
    #These computed fields are for calculating the domain on a form edit 
    calcislocaluom = fields.Boolean('Find if its a localuom',compute='_computelocaluom', store=True, default=False) 
    calccatidname = fields.Char('Find the name of the category id', compute='_computecatidname', store=True,default=True) 
    #[...] other code removed 

    @api.one 
    @api.depends('uom_class') 
    def _computelocaluom(self): 
     if (self.uom_class): 
      self.calcislocaluom = True 
      return True 
     else: 
      self.calcislocaluom = False 
      return False 

    @api.one 
    @api.depends('uom_class') 
    def _computecatidname(self): 
     if (self.uom_class): 
      self.calccatidname = self.uom_class.name 
      return self.uom_class.name 
     else: 
      #Due to the conditions we later impose within the view, we need to specify a category name that will always be there 
      self.calccatidname = "Unsorted/Imported Units" 
      return True 

我要暫緩標誌着這是一個正確的答案,因爲它夠硬創建靜態定義域,實際上做什麼,我想他們,並根據數據動態行爲...但是不得不使用反向波蘭語來表達這些複雜的陳述就是酷刑。