英文:
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'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_type
和 is_company
之间存在关联。还需要注意的是。
英文:
company_type
is computed so you should also change the related methods, for example the compute method:
@api.depends('is_company')
def _compute_company_type(self):
for partner in self:
partner.company_type = 'company' if partner.is_company else 'person'
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 = "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
Hope this will work fine so far.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论