这是最好的,我可以拿出来,使用计算字段。下面是从我的代码
在我的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="['&',('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
我要暂缓标志着这是一个正确的答案,因为它够硬创建静态定义域,实际上做什么,我想他们,并根据数据动态行为...但是不得不使用反向波兰语来表达这些复杂的陈述就是酷刑。
我真的不明白你的意思,如果字段'y'取决于字段'x',如果x不变,字段'y'中的任何变化如何被触发? *动态*(改变的东西)域必须基于变化的东西。我怀疑你想要一个默认域名,但直到你澄清之前我永远都不会知道。 – danidee
好吧,说白了,我有一个改变,就是我在问题中所描述的。当我创造新纪录时它效果很好。但是,在保存记录并重新编辑之后,该域将重置为默认值。 从用户的角度来看,这确实没有任何意义。当他们创造记录时,他们给“最喜欢的玩具”的选择是他们选择的动物所特有的。现在他们去改变“最喜欢的玩具”,但Odoo给他们提供了一个不同的选择领域。他们应该看到他们最后一次的选择。 – Nross2781
好吧,所以我有想做一个计算字段,并用它来帮助设置域中的变量....但我不能让我的生活得到计算字段存储在数据库中。即使store = True,值也不会被保存。 我发誓,更新的API是真气。我看到所有这些适用于v7的解决方案,但在v9中没有办法做同样的事情。 – Nross2781