处理od中的多个字段

2024-09-28 21:54:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我有两个模型技能和用户。在

我希望每个用户可以有许多技能,而且每个技能可以有许多用户在它上面。我试过那样做,但没有成功。在

这是我的技能:

class technicians_skills(osv.osv):
_name = 'skills'
_description = 'Technicians Skills'

_columns = {
    'name': fields.char(string='name', size=50),
    'description': fields.text(string="description"),
    'member_ids': fields.many2many('res.users', 'skill', string='Technicians')
} 

这是用户:

^{pr2}$

我想知道每个用户的技能,但当我称之为:

test = http.request.env['skills.rel'].search([])

它显示了这个错误

KeyError: 'skills.rel'


Tags: columns用户name模型fieldsstring技能description
1条回答
网友
1楼 · 发布于 2024-09-28 21:54:19

您需要指定声明中的所有关键字才能创建相同的关系表

   fields.many2many(
      comodel_name='model.name',
      relation='valid_postgres_name',
      colum1='current_model_m2o_field_name',
      column2='comodel_m2o_name',
      string='Label')

而在另一个定义中,保持关系的名称相同,但与另一个关键字相反。在

在技能上

^{pr2}$

在用户中

skill_ids = fields.many2many('skills',  'user_skill_rel', 'skill_id', 'user_id', 'Skills')

你看我是如何颠倒定义的,只是关系是一样的。 保持列的名称相同

编辑:

不要对关系执行搜索,因为它们不是模型。您需要对模型执行搜索,然后访问多个字段。在

假设你想获得当前用户的技能。在

   self.env.user.skill_ids  # this will return the list of the skills for the current user

如果你想获得多个用户的技能。在

  result = self.env['res.users'].search([your_domain]])

  # if you need to show user information then it's skill
  for rec in result:
       # use loop
       rec.name # user name
       rec.skill_ids

  # but if you want to get the list of skill and you don't need users
  skills = result.mapped('skills_ids')  # mapped will return all the skills without duplication

相关问题 更多 >