2016-09-18 54 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 

我要暂缓标志着这是一个正确的答案,因为它够硬创建静态定义域,实际上做什么,我想他们,并根据数据动态行为...但是不得不使用反向波兰语来表达这些复杂的陈述就是酷刑。