继承的选择字段在Odoo 15中没有被选择。

huangapple go评论63阅读模式
英文:

Inherited selection field isn't selecting in odoo 15

问题

我已经继承了res.partner模型,并在company_type字段中添加了2个选择值,如下所示:

```python
class InheritContract(models.Model):
    _inherit = "res.partner"

    company_type = fields.Selection(selection_add=[('vendor', '供应商'),('panel', '面板')])

这两个选择值显示出来了,但我无法选择它们。有人有解决办法吗?
视频链接中的问题


<details>
<summary>英文:</summary>

I&#39;ve inherited the res.partner model and added 2 selection value in company_type field as like below

class InheritContract(models.Model):
_inherit = "res.partner"

company_type = fields.Selection(selection_add=[('vendor', 'Vendor'),('panel', 'Panel')])


These two selection value is showing but I can not select them. Does anyone have any solution? 
[problem in the video link][1]


  [1]: https://youtu.be/53ut0d8KrQE

</details>


# 答案1
**得分**: 1

`company_type` 是计算出来的,所以你也应该修改相关的方法,例如 `compute` 方法:

```py
    @api.depends('is_company')
    def _compute_company_type(self):
        for partner in self:
            partner.company_type = 'company' if partner.is_company else 'person'

你还可以看到字段 company_typeis_company 之间存在关联。还需要注意的是。

英文:

company_type is computed so you should also change the related methods, for example the compute method:

    @api.depends(&#39;is_company&#39;)
    def _compute_company_type(self):
        for partner in self:
            partner.company_type = &#39;company&#39; if partner.is_company else &#39;person&#39;

You also can see that there is a relation between the fields company_type and is_company. More you have to take care of.

答案2

得分: 0

我找到了解决方案。如果有人想在Odoo联系人(res.partner)中添加更多的联系人类型,以下是代码部分:

class InheritContract(models.Model):
    _inherit = "res.partner"
    is_vendor = fields.Boolean(string='Is a Vendor', default=False,
                               help="Check if the contact is a Vendor, otherwise it is a person/company")
    is_panel = fields.Boolean(string='Is a Panel', default=False,
                              help="Check if the contact is a Panel, otherwise it is a person/company/vendor")

    company_type = fields.Selection(selection_add=[('vendor', 'Vendor'),('panel', 'Panel')],
                                    compute='_compute_company_type', inverse='_write_company_type')

    @api.depends('is_company')
    def _compute_company_type(self):
        for partner in self:
            if partner.is_company:
                partner.company_type = 'company'
            elif partner.is_vendor:
                partner.company_type = 'vendor'
            elif partner.is_panel:
                partner.company_type = 'panel'
            else:
                partner.company_type = 'person'

    def _write_company_type(self):
        for partner in self:
            if partner.company_type == 'panel':
                partner.is_panel = True
                partner.is_company = False
                partner.is_vendor = False
            elif partner.company_type == 'vendor':
                partner.is_vendor = True
                partner.is_panel = False
                partner.is_company = False
            elif partner.company_type == 'company':
                partner.is_company = True
                partner.is_vendor = False
                partner.is_panel = False
            else:
                partner.is_company = False
                partner.is_vendor = False
                partner.is_panel = False

    @api.onchange('company_type')
    def onchange_company_type(self):
        if self.company_type == 'panel':
            self.is_panel = True
            self.is_vendor = False
            self.is_company = False
        elif self.company_type == 'vendor':
            self.is_vendor = True
            self.is_panel = False
            self.is_company = False
        elif self.company_type == 'company':
            self.is_company = True
            self.is_vendor = False
            self.is_panel = False
        else:
            self.is_company = False
            self.is_vendor = False
            self.is_panel = False

希望这个代码部分能够正常工作。

英文:

I found the solution. If anyone want to add more Contacts Type in Odoo Contact(res.partner). Here is the code

class InheritContract(models.Model):
_inherit = &quot;res.partner&quot;
is_vendor = fields.Boolean(string=&#39;Is a Vendor&#39;, default=False,
help=&quot;Check if the contact is a Vendor, otherwise it is a person/company&quot;)
is_panel = fields.Boolean(string=&#39;Is a Panel&#39;, default=False,
help=&quot;Check if the contact is a Panel, otherwise it is a person/company/vendor&quot;)
company_type = fields.Selection(selection_add=[(&#39;vendor&#39;, &#39;Vendor&#39;),(&#39;panel&#39;, &#39;Panel&#39;)],
compute=&#39;_compute_company_type&#39;,inverse=&#39;_write_company_type&#39;)
@api.depends(&#39;is_company&#39;)
def _compute_company_type(self):
for partner in self:
if partner.is_company:
partner.company_type = &#39;company&#39;
elif partner.is_vendor:
partner.company_type = &#39;vendor&#39;
elif partner.is_panel:
partner.company_type = &#39;panel&#39;
else:
partner.company_type = &#39;person&#39;
def _write_company_type(self):
for partner in self:
if partner.company_type == &#39;panel&#39;:
partner.is_panel = True
partner.is_company = False
partner.is_vendor = False
elif partner.company_type == &#39;vendor&#39;:
partner.is_vendor = True
partner.is_panel = False
partner.is_company = False
elif partner.company_type == &#39;company&#39;:
partner.is_company = True
partner.is_vendor = False
partner.is_panel = False
else:
partner.is_company = False
partner.is_vendor = False
partner.is_panel = False
@api.onchange(&#39;company_type&#39;)
def onchange_company_type(self):
if self.company_type == &#39;panel&#39;:
self.is_panel = True
self.is_vendor = False
self.is_company = False
elif self.company_type == &#39;vendor&#39;:
self.is_vendor = True
self.is_panel = False
self.is_company = False
elif self.company_type == &#39;company&#39;:
self.is_company = True
self.is_vendor = False
self.is_panel = False
else:
self.is_company = False
self.is_vendor = False
self.is_panel = False

Hope this will work fine so far.

huangapple
  • 本文由 发表于 2023年6月12日 14:48:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/76454175.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定