简体   繁体   English

如何为Odoo 10字段创建动态域(在表单加载之前会给出)?

[英]How to make dynamic domain for Odoo 10 field (that would be given before form loads)?

What I have is two fields on them is customer and the other is customer contact: 我所拥有的是两个领域是客户,另一个是客户联系:

   customer = fields.Many2one(res.partner)
   customer_contact = fields.Many2one(res.partner)

## What I made so far is: ##我到目前为止所做的是:

    @api.onchange('customer')
    def _getCustomerContacts(self):
    res = {}
    self.customer_contact = False
    if self.customer:
         res['domain'] = {
            'customer_contact': [('id', 'in', 
    self.carrier.child_ids.ids),('type', '=', 'driver')]}
    else:
        res['domain'] = {'customer_contact': [('id', 'in', [])]}
    return res

This is bad solution for one reason: If i come later to form and try to change only contact, he wont have a domain, and all res partner will be displayed. 这是一个糟糕的解决方案,原因之一是:如果我稍后来形成并尝试仅更改联系人,则他将不会拥有域名,并且将显示所有res合作伙伴。 I searched for answer for a while now and seems like everybody is fine with onchange solution. 我现在搜索了一段时间的答案,似乎每个人都可以使用onchange解决方案。 For me is not fine. 对我来说不是很好。 So Maybe there would be a nice guy who had similar problem and found solution or maybe you have some suggestion what should I look for. 所以也许会有一个好人有类似的问题并找到解决方案或者你有一些建议我应该寻找什么。

Thank you very much for your time!!! 非常感谢您的宝贵时间!!!

You can try : 你可以试试 :

@api.onchange('customer')
def _getCustomerContacts(self):
if self.customer:
    ids = []
    for child_id in self.carrier.child_ids:
        ids.append(child_id.id)

    domain = {
        'customer_contact': [('id', 'in', ids),('type', '=', 'driver')]}
else:
    domain = {'customer_contact': [('id', 'in', [])]}
return domain

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM