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

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

Inherited selection field isn't selecting in odoo 15

问题

  1. 我已经继承了res.partner模型,并在company_type字段中添加了2个选择值,如下所示:
  2. ```python
  3. class InheritContract(models.Model):
  4. _inherit = "res.partner"
  5. company_type = fields.Selection(selection_add=[('vendor', '供应商'),('panel', '面板')])

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

  1. <details>
  2. <summary>英文:</summary>
  3. 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')])

  1. These two selection value is showing but I can not select them. Does anyone have any solution?
  2. [problem in the video link][1]
  3. [1]: https://youtu.be/53ut0d8KrQE
  4. </details>
  5. # 答案1
  6. **得分**: 1
  7. `company_type` 是计算出来的,所以你也应该修改相关的方法,例如 `compute` 方法:
  8. ```py
  9. @api.depends('is_company')
  10. def _compute_company_type(self):
  11. for partner in self:
  12. 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:

  1. @api.depends(&#39;is_company&#39;)
  2. def _compute_company_type(self):
  3. for partner in self:
  4. 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)中添加更多的联系人类型,以下是代码部分:

  1. class InheritContract(models.Model):
  2. _inherit = "res.partner"
  3. is_vendor = fields.Boolean(string='Is a Vendor', default=False,
  4. help="Check if the contact is a Vendor, otherwise it is a person/company")
  5. is_panel = fields.Boolean(string='Is a Panel', default=False,
  6. help="Check if the contact is a Panel, otherwise it is a person/company/vendor")
  7. company_type = fields.Selection(selection_add=[('vendor', 'Vendor'),('panel', 'Panel')],
  8. compute='_compute_company_type', inverse='_write_company_type')
  9. @api.depends('is_company')
  10. def _compute_company_type(self):
  11. for partner in self:
  12. if partner.is_company:
  13. partner.company_type = 'company'
  14. elif partner.is_vendor:
  15. partner.company_type = 'vendor'
  16. elif partner.is_panel:
  17. partner.company_type = 'panel'
  18. else:
  19. partner.company_type = 'person'
  20. def _write_company_type(self):
  21. for partner in self:
  22. if partner.company_type == 'panel':
  23. partner.is_panel = True
  24. partner.is_company = False
  25. partner.is_vendor = False
  26. elif partner.company_type == 'vendor':
  27. partner.is_vendor = True
  28. partner.is_panel = False
  29. partner.is_company = False
  30. elif partner.company_type == 'company':
  31. partner.is_company = True
  32. partner.is_vendor = False
  33. partner.is_panel = False
  34. else:
  35. partner.is_company = False
  36. partner.is_vendor = False
  37. partner.is_panel = False
  38. @api.onchange('company_type')
  39. def onchange_company_type(self):
  40. if self.company_type == 'panel':
  41. self.is_panel = True
  42. self.is_vendor = False
  43. self.is_company = False
  44. elif self.company_type == 'vendor':
  45. self.is_vendor = True
  46. self.is_panel = False
  47. self.is_company = False
  48. elif self.company_type == 'company':
  49. self.is_company = True
  50. self.is_vendor = False
  51. self.is_panel = False
  52. else:
  53. self.is_company = False
  54. self.is_vendor = False
  55. 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

  1. class InheritContract(models.Model):
  2. _inherit = &quot;res.partner&quot;
  3. is_vendor = fields.Boolean(string=&#39;Is a Vendor&#39;, default=False,
  4. help=&quot;Check if the contact is a Vendor, otherwise it is a person/company&quot;)
  5. is_panel = fields.Boolean(string=&#39;Is a Panel&#39;, default=False,
  6. help=&quot;Check if the contact is a Panel, otherwise it is a person/company/vendor&quot;)
  7. company_type = fields.Selection(selection_add=[(&#39;vendor&#39;, &#39;Vendor&#39;),(&#39;panel&#39;, &#39;Panel&#39;)],
  8. compute=&#39;_compute_company_type&#39;,inverse=&#39;_write_company_type&#39;)
  9. @api.depends(&#39;is_company&#39;)
  10. def _compute_company_type(self):
  11. for partner in self:
  12. if partner.is_company:
  13. partner.company_type = &#39;company&#39;
  14. elif partner.is_vendor:
  15. partner.company_type = &#39;vendor&#39;
  16. elif partner.is_panel:
  17. partner.company_type = &#39;panel&#39;
  18. else:
  19. partner.company_type = &#39;person&#39;
  20. def _write_company_type(self):
  21. for partner in self:
  22. if partner.company_type == &#39;panel&#39;:
  23. partner.is_panel = True
  24. partner.is_company = False
  25. partner.is_vendor = False
  26. elif partner.company_type == &#39;vendor&#39;:
  27. partner.is_vendor = True
  28. partner.is_panel = False
  29. partner.is_company = False
  30. elif partner.company_type == &#39;company&#39;:
  31. partner.is_company = True
  32. partner.is_vendor = False
  33. partner.is_panel = False
  34. else:
  35. partner.is_company = False
  36. partner.is_vendor = False
  37. partner.is_panel = False
  38. @api.onchange(&#39;company_type&#39;)
  39. def onchange_company_type(self):
  40. if self.company_type == &#39;panel&#39;:
  41. self.is_panel = True
  42. self.is_vendor = False
  43. self.is_company = False
  44. elif self.company_type == &#39;vendor&#39;:
  45. self.is_vendor = True
  46. self.is_panel = False
  47. self.is_company = False
  48. elif self.company_type == &#39;company&#39;:
  49. self.is_company = True
  50. self.is_vendor = False
  51. self.is_panel = False
  52. else:
  53. self.is_company = False
  54. self.is_vendor = False
  55. 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:

确定