【问题标题】:deal with many2many field in odoo处理odoo中的many2many字段
【发布时间】:2018-04-03 15:11:45
【问题描述】:

我有两个模型技能和 res_users。

我希望每个用户都可以拥有许多技能,并且每个技能都可以拥有许多用户。我试图这样做,但没有成功。

这是我的技能:

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')
} 

这是用户:

class res_users(osv.osv):
_inherit = 'res.users'

_columns = {
   'skill': fields.many2many('skills', relation='skills.rel', column1='name', column2='skill', string='Skill'),
}

我想知道每个用户的技能,但是当我这样称呼时:

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

它显示了这个错误

KeyError: 'skills.rel'

【问题讨论】:

    标签: python odoo


    【解决方案1】:

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

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

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

    在技能方面

      user_ids = fields.many2many('re.users', 'user_skill_rel','user_id','skill_id', 'Users')
    

    在用户中

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

    看看我是如何反转定义的,只有关系是相同的。 保持列名相同

    编辑:

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

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

       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
    

    【讨论】:

    • 要查找用户的技能,我需要向 user_skill_rel 提出请求?
    • No on Skills then accesses you many2many field 这个问题意味着你是 Odoo 的新手。您至少需要先学习基础知识
    • 我的问题是如何使用 ORM 获取我的值
    • 希望你现在能明白这一点
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-09
    • 1970-01-01
    • 2022-06-16
    • 2019-11-20
    相关资源
    最近更新 更多