for*_*vas 5 python python-2.7 odoo odoo-8
我创建了一个名为 的新模型product.service.type。然后,在product.product模型中,我还创建了一个Many2many字段(名为service_type,指向product.service.type模型)。
现在我有了 model test,它有product_id和service_type_id字段,分别Many2one指向product.product和product.service.type。
我想要的是,如果您选择一个产品,服务类型域将更改为仅显示所选产品的服务类型。我通过以下方式做到了这一点onchange:
def onchange_product_id(self, cr, uid, ids, product_id, context=None):
if product_id:
product = self.pool.get('product.product').browse(
cr, uid, [product_id], context=context)
service_type_ids = product.service_type.mapped('id')
return {
'domain': {
'service_type_id': [('id', 'in', service_type_ids)],
},
}
Run Code Online (Sandbox Code Playgroud)
这很好用,问题是当您编辑记录(而不是创建新记录)时,因为在这种情况下,onchange未执行记录,因此域显示所有服务类型。
您可以在合作伙伴表单中使用字段看到同样的问题title。新建一个合作伙伴,该合作伙伴是公司,字段的域title发生变化,只允许选择Corp.、Ltd.等记录,但如果设置合作伙伴是联系人,则可以选择Doctor、Madam等记录现在,将您想要的数据保存到伙伴中,然后转到顶部栏的其他菜单。返回合作伙伴表单并打开创建的合作伙伴进行编辑。检查title字段而不更改is_company字段。现在您拥有所有可用的头衔,尽管您的合作伙伴属于特定类别(公司或联系人)。
我该如何解决这个问题?
我认为,列出以下几点。是可以实现的。
例如:
@api.model
def search(self, args, offset=0, limit=None, order=None, count=False):
context = self._context or {}
# Display product list which has not included in CofA Template
if context.get('product_service_id'):
product = self.env['product.product'].browse(context.get('product_service_id'))
service_type_ids = product.service_type.mapped('id')
args += [('service_type_id', 'not in', service_type_ids)]
return super(ProductServiceType, self).search(args,
offset,
limit,
order,
count=count)
Run Code Online (Sandbox Code Playgroud)
在 XML 方面:
<field name="product_id"/>
<field name="many2many_field" context="{'product_service_id': product_id}">
Run Code Online (Sandbox Code Playgroud)
注意:我已尝试按照新的 API 进行回答,但尚未对其进行测试。您需要使用旧 API 或根据您的要求进行转换。
| 归档时间: |
|
| 查看次数: |
1225 次 |
| 最近记录: |