gpt4 book ai didi

python - Openerp : Iterate Many2Many field

转载 作者:太空宇宙 更新时间:2023-11-04 05:32:42 24 4
gpt4 key购买 nike

这是我的类(class):

employee_ids = fields.Many2many('hr.employee', string="Empls")
status = fields.Selection([
('draft', 'Draft'),
('done', 'Done'),
])

然后在 fields_view_get 方法中,我想遍历 employee_ids 并列出每个员工。

def fields_view_get(self, cr, uid, view_id=None, view_type='form', context=None, toolbar=False, submenu=False):
if context is None:
context = {}
res = super(help_desk, self).fields_view_get(cr, uid, view_id=view_id, view_type=view_type, context=context, toolbar=toolbar,submenu=False)
#here i want to iterate throught employee_ids and make list of each employee
for f in res['fields']:
if f == 'status':
res['fields'][f]['selection'] = #put list here
return res

我该怎么做?谢谢

最佳答案

我假设 help_desk 模型有 3 个字段 Selection、Many2one 和 Many2many:

        status = fields.Selection([
('draft', 'Draft'),
('done', 'Done'),
])
partner_id = fields.Many2one(comodel_name='res.partner', string='Partner')
employee_ids = fields.Many2many('hr.employee', string="Empls")

现在,如果您想应用一些逻辑,请阅读下面提到的几行。

fields_get ,fields_view_get 这样的好方法可以帮助我们通过动态应用过滤器/域来改善 UI 体验。所以代码在这里:

@api.model
def fields_view_get(self, view_id=None, view_type='form', toolbar=False, submenu=False):
res = super(help_desk, self).fields_view_get(view_id=view_id, view_type=view_type, toolbar=toolbar, submenu=submenu)
PartnerObj= self.env['res.partner']
domain = [('phone','!=',False)]# put your domain or just place blank list
partners = PartnerObj.search(domain)
if partners
for field in res['fields']:
# if field == 'partner_id':
# res['fields'][field]['domain'] = [('id', 'in', partners.ids)]
elif field == 'employee_ids':
res['fields'][field]['domain'] = [('id', 'in', partners.ids)]
elif field=='status':
# Appending the partners in status doesn't making any sense but as per your words "put list here"
res['fields'][field]['selection'] = partners and [(partner.id, partner.name) for partner in partners] or [('', '')]

return res

这里我将域放在 Many2one 和 Many2many 上,并在 Selection 字段中添加一些基于域的动态列表。

您也可以引用account/models/chart_template.py.

关于python - Openerp : Iterate Many2Many field,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36592727/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com