Python Odoo v9-编辑表单时设置动态域(不创建)

Python Odoo v9-编辑表单时设置动态域(不创建),python,openerp,views,Python,Openerp,Views,假设您正在设置一个名为“Animal Type”的字段,并且有一个依赖于该字段的字段名为“Favorite toy”。如果“动物类型”是狗,我想将“最喜欢的玩具”的域设置为类似('isdogtoy','=',True)的内容。如果它是一只猫,那么我们可能会将其设置为False或其他条件 通常要设置动态域,您需要使用onchange,然后为字段设置域 然而,在某些情况下,一切都不会改变。例如,如果编辑现有记录,则不必调用onchange。如果我从不更改“动物类型”,那么我的“最喜欢的玩具”域就永远

假设您正在设置一个名为“Animal Type”的字段,并且有一个依赖于该字段的字段名为“Favorite toy”。如果“动物类型”是狗,我想将“最喜欢的玩具”的域设置为类似('isdogtoy','=',True)的内容。如果它是一只猫,那么我们可能会将其设置为False或其他条件

通常要设置动态域,您需要使用onchange,然后为字段设置域

然而,在某些情况下,一切都不会改变。例如,如果编辑现有记录,则不必调用onchange。如果我从不更改“动物类型”,那么我的“最喜欢的玩具”域就永远不会通过onchange方法设置


我不确定我们在奥多是如何动态地做到这一点的。看起来很明显应该有办法,但我在这上面找不到任何东西

这是我能想到的最好的方法,使用计算字段。下面是我的代码中的一个示例解决方案

在我的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>

我将暂缓将此标记为正确答案,因为它很难创建静态定义的域,这些域实际执行我希望它们执行的操作,并根据数据动态地执行操作。。。。但是,必须用波兰语的反符号来表达这些复杂的语句简直是一种折磨。

我真的不明白你的意思,如果字段
y
依赖于字段
x
,如果x不改变,字段
y
中的任何变化怎么可能被触发呢?。动态(变化的东西)域必须基于变化的东西。我怀疑你想要一个默认域,但在你澄清之前我永远都不知道。好吧,说得明白一点,我有一个onchange,它实现了我在问题中描述的。当我录制新唱片时,效果非常好。但是,在我保存记录并返回并编辑它之后,域将重置为默认值。从用户的角度来看,这真的没有任何意义。当他们创造记录时,给他们的“最喜欢的玩具”的选择是针对他们选择的动物的。现在他们去换“最喜欢的玩具”,但奥多给他们提供了不同的选择领域。他们应该看到与上次相同的选择。好的,所以我想到了制作一个计算字段,并用它来帮助设置域中的变量。。。。但是,我一生都无法将计算字段存储在数据库中。即使使用store=True,也不会保存值。我发誓,新的api令人愤怒。我看到所有这些解决方案都适用于v7,但在v9中没有相同的方法。向我们展示计算字段解决方案的代码,也许我们可以解决一些问题:-)计算字段也必须使用
@api。依赖
装饰器。但在给出任何答案之前,我们必须先查看一些代码(完整的代码以及xml文件中的相关视图)
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