Odoo中文社区可以通过以下两个域名访问:shine-it.net , odoo.net.cn

由于系统升迁的原因,本论坛部分较早期的内容存在格式和链接损坏失效的问题,并非本论坛系统本身的缺陷,望谅解

本社区没有维护任何QQ群讨论组,任何与本社区同名的QQ群讨论组的言论与本社区无关!

开发人员可以登录gitter讨论组: http://gitter.im/odoo-china/Talk, 需要github账号

如果您登录系统碰到问题,请在微信公众号留言:

字段domain过滤权限问题,请各位大大看看



  • @digitalsatori字段domain过滤权限问题,请各位大大看看 中说:

    domain=lambda self: [('groups_id.id', '=', self.env.ref('权限组xmlid').id)]

    因为我之前试了下,在字段里和在view里效果一样,所以就写view里了,方便更新。
    您这个我试了下还是报错:

    Odoo Server Error
    Traceback (most recent call last):
      File "E:\workdata\PycharmProjects\odoo-12\odoo\tools\cache.py", line 88, in lookup
        r = d[key]
      File "E:\workdata\PycharmProjects\odoo-12\odoo\tools\func.py", line 69, in wrapper
        return func(self, *args, **kwargs)
      File "E:\workdata\PycharmProjects\odoo-12\odoo\tools\lru.py", line 44, in __getitem__
        a = self.d[obj].me
    KeyError: ('ir.model.data', <function IrModelData.xmlid_lookup at 0x0000000003D5D798>, 'yz_group_bm')
    
    During handling of the above exception, another exception occurred:
    
    Traceback (most recent call last):
      File "E:\workdata\PycharmProjects\odoo-12\odoo\models.py", line 1108, in _validate_fields
        check(self)
      File "e:\workdata\pycharmprojects\odoo-12\odoo\addons\base\models\ir_ui_view.py", line 351, in _check_xml
        self.postprocess_and_fields(view.model, view_doc, view.id)
      File "e:\workdata\pycharmprojects\odoo-12\odoo\addons\base\models\ir_ui_view.py", line 1085, in postprocess_and_fields
        fields = Model.fields_get(None)
      File "E:\workdata\PycharmProjects\odoo-12\odoo\models.py", line 2684, in fields_get
        description = field.get_description(self.env)
      File "E:\workdata\PycharmProjects\odoo-12\odoo\fields.py", line 746, in get_description
        value = value(env)
      File "E:\workdata\PycharmProjects\odoo-12\odoo\fields.py", line 2049, in _description_domain
        return self.domain(env[self.model_name]) if callable(self.domain) else self.domain
      File "e:\workdata\pycharmprojects\odoo-12\odoo\myaddons\business_entertainment\models\business_entertainment_sheet.py", line 28, in <lambda>
        domain=lambda self: [('groups_id.id', '=', self.env.ref('yz_group_bm').id)])
      File "E:\workdata\PycharmProjects\odoo-12\odoo\api.py", line 854, in ref
        return self['ir.model.data'].xmlid_to_object(xml_id, raise_if_not_found=raise_if_not_found)
      File "e:\workdata\pycharmprojects\odoo-12\odoo\addons\base\models\ir_model.py", line 1420, in xmlid_to_object
        t = self.xmlid_to_res_model_res_id(xmlid, raise_if_not_found)
      File "e:\workdata\pycharmprojects\odoo-12\odoo\addons\base\models\ir_model.py", line 1404, in xmlid_to_res_model_res_id
        return self.xmlid_lookup(xmlid)[1:3]
      File "<decorator-gen-25>", line 2, in xmlid_lookup
      File "E:\workdata\PycharmProjects\odoo-12\odoo\tools\cache.py", line 93, in lookup
        value = d[key] = self.method(*args, **kwargs)
      File "e:\workdata\pycharmprojects\odoo-12\odoo\addons\base\models\ir_model.py", line 1390, in xmlid_lookup
        module, name = xmlid.split('.', 1)
    ValueError: not enough values to unpack (expected 2, got 1)
    
    During handling of the above exception, another exception occurred:
    
    Traceback (most recent call last):
      File "E:\workdata\PycharmProjects\odoo-12\odoo\tools\convert.py", line 758, in parse
        self._tags[rec.tag](rec, de, mode=mode)
      File "E:\workdata\PycharmProjects\odoo-12\odoo\tools\convert.py", line 663, in _tag_record
        record = model.with_context(rec_context)._load_records([data], self.mode == 'update')
      File "E:\workdata\PycharmProjects\odoo-12\odoo\models.py", line 3838, in _load_records
        data['record']._load_records_write(data['values'])
      File "E:\workdata\PycharmProjects\odoo-12\odoo\models.py", line 3776, in _load_records_write
        self.write(values)
      File "e:\workdata\pycharmprojects\odoo-12\odoo\addons\base\models\ir_ui_view.py", line 444, in write
        return super(View, self).write(self._compute_defaults(vals))
      File "E:\workdata\PycharmProjects\odoo-12\odoo\models.py", line 3347, in write
        fields[0].determine_inverse(records)
      File "E:\workdata\PycharmProjects\odoo-12\odoo\fields.py", line 1117, in determine_inverse
        getattr(records, self.inverse)()
      File "e:\workdata\pycharmprojects\odoo-12\odoo\addons\base\models\ir_ui_view.py", line 275, in _inverse_arch
        view.write(data)
      File "e:\workdata\pycharmprojects\odoo-12\odoo\addons\base\models\ir_ui_view.py", line 444, in write
        return super(View, self).write(self._compute_defaults(vals))
      File "E:\workdata\PycharmProjects\odoo-12\odoo\models.py", line 3321, in write
        self._write(store_vals)
      File "E:\workdata\PycharmProjects\odoo-12\odoo\models.py", line 3457, in _write
        self._validate_fields(vals)
      File "E:\workdata\PycharmProjects\odoo-12\odoo\models.py", line 1112, in _validate_fields
        raise ValidationError("%s\n\n%s" % (_("Error while validating constraint"), tools.ustr(e)))
    odoo.exceptions.ValidationError: ('constraint 检查错误\n\nnot enough values to unpack (expected 2, got 1)', None)
    
    During handling of the above exception, another exception occurred:
    
    Traceback (most recent call last):
      File "E:\workdata\PycharmProjects\odoo-12\odoo\http.py", line 656, in _handle_exception
        return super(JsonRequest, self)._handle_exception(exception)
      File "E:\workdata\PycharmProjects\odoo-12\odoo\http.py", line 314, in _handle_exception
        raise pycompat.reraise(type(exception), exception, sys.exc_info()[2])
      File "E:\workdata\PycharmProjects\odoo-12\odoo\tools\pycompat.py", line 87, in reraise
        raise value
      File "E:\workdata\PycharmProjects\odoo-12\odoo\http.py", line 698, in dispatch
        result = self._call_function(**self.params)
      File "E:\workdata\PycharmProjects\odoo-12\odoo\http.py", line 346, in _call_function
        return checked_call(self.db, *args, **kwargs)
      File "E:\workdata\PycharmProjects\odoo-12\odoo\service\model.py", line 97, in wrapper
        return f(dbname, *args, **kwargs)
      File "E:\workdata\PycharmProjects\odoo-12\odoo\http.py", line 339, in checked_call
        result = self.endpoint(*a, **kw)
      File "E:\workdata\PycharmProjects\odoo-12\odoo\http.py", line 941, in __call__
        return self.method(*args, **kw)
      File "E:\workdata\PycharmProjects\odoo-12\odoo\http.py", line 519, in response_wrap
        response = f(*args, **kw)
      File "e:\workdata\pycharmprojects\odoo-12\odoo\addons\web\controllers\main.py", line 966, in call_button
        action = self._call_kw(model, method, args, {})
      File "e:\workdata\pycharmprojects\odoo-12\odoo\addons\web\controllers\main.py", line 954, in _call_kw
        return call_kw(request.env[model], method, args, kwargs)
      File "E:\workdata\PycharmProjects\odoo-12\odoo\api.py", line 749, in call_kw
        return _call_kw_multi(method, model, args, kwargs)
      File "E:\workdata\PycharmProjects\odoo-12\odoo\api.py", line 736, in _call_kw_multi
        result = method(recs, *args, **kwargs)
      File "<decorator-gen-67>", line 2, in button_immediate_upgrade
      File "e:\workdata\pycharmprojects\odoo-12\odoo\addons\base\models\ir_module.py", line 74, in check_and_log
        return method(self, *args, **kwargs)
      File "e:\workdata\pycharmprojects\odoo-12\odoo\addons\base\models\ir_module.py", line 622, in button_immediate_upgrade
        return self._button_immediate_function(type(self).button_upgrade)
      File "e:\workdata\pycharmprojects\odoo-12\odoo\addons\base\models\ir_module.py", line 561, in _button_immediate_function
        modules.registry.Registry.new(self._cr.dbname, update_module=True)
      File "E:\workdata\PycharmProjects\odoo-12\odoo\modules\registry.py", line 86, in new
        odoo.modules.load_modules(registry._db, force_demo, status, update_module)
      File "E:\workdata\PycharmProjects\odoo-12\odoo\modules\loading.py", line 417, in load_modules
        force, status, report, loaded_modules, update_module, models_to_check)
      File "E:\workdata\PycharmProjects\odoo-12\odoo\modules\loading.py", line 313, in load_marked_modules
        perform_checks=perform_checks, models_to_check=models_to_check
      File "E:\workdata\PycharmProjects\odoo-12\odoo\modules\loading.py", line 222, in load_module_graph
        load_data(cr, idref, mode, kind='data', package=package, report=report)
      File "E:\workdata\PycharmProjects\odoo-12\odoo\modules\loading.py", line 68, in load_data
        tools.convert_file(cr, package.name, filename, idref, mode, noupdate, kind, report)
      File "E:\workdata\PycharmProjects\odoo-12\odoo\tools\convert.py", line 802, in convert_file
        convert_xml_import(cr, module, fp, idref, mode, noupdate, report)
      File "E:\workdata\PycharmProjects\odoo-12\odoo\tools\convert.py", line 865, in convert_xml_import
        obj.parse(doc.getroot(), mode=mode)
      File "E:\workdata\PycharmProjects\odoo-12\odoo\tools\convert.py", line 764, in parse
        exc_info[2]
      File "E:\workdata\PycharmProjects\odoo-12\odoo\tools\pycompat.py", line 86, in reraise
        raise value.with_traceback(tb)
      File "E:\workdata\PycharmProjects\odoo-12\odoo\tools\convert.py", line 758, in parse
        self._tags[rec.tag](rec, de, mode=mode)
      File "E:\workdata\PycharmProjects\odoo-12\odoo\tools\convert.py", line 663, in _tag_record
        record = model.with_context(rec_context)._load_records([data], self.mode == 'update')
      File "E:\workdata\PycharmProjects\odoo-12\odoo\models.py", line 3838, in _load_records
        data['record']._load_records_write(data['values'])
      File "E:\workdata\PycharmProjects\odoo-12\odoo\models.py", line 3776, in _load_records_write
        self.write(values)
      File "e:\workdata\pycharmprojects\odoo-12\odoo\addons\base\models\ir_ui_view.py", line 444, in write
        return super(View, self).write(self._compute_defaults(vals))
      File "E:\workdata\PycharmProjects\odoo-12\odoo\models.py", line 3347, in write
        fields[0].determine_inverse(records)
      File "E:\workdata\PycharmProjects\odoo-12\odoo\fields.py", line 1117, in determine_inverse
        getattr(records, self.inverse)()
      File "e:\workdata\pycharmprojects\odoo-12\odoo\addons\base\models\ir_ui_view.py", line 275, in _inverse_arch
        view.write(data)
      File "e:\workdata\pycharmprojects\odoo-12\odoo\addons\base\models\ir_ui_view.py", line 444, in write
        return super(View, self).write(self._compute_defaults(vals))
      File "E:\workdata\PycharmProjects\odoo-12\odoo\models.py", line 3321, in write
        self._write(store_vals)
      File "E:\workdata\PycharmProjects\odoo-12\odoo\models.py", line 3457, in _write
        self._validate_fields(vals)
      File "E:\workdata\PycharmProjects\odoo-12\odoo\models.py", line 1112, in _validate_fields
        raise ValidationError("%s\n\n%s" % (_("Error while validating constraint"), tools.ustr(e)))
    odoo.tools.convert.ParseError: "constraint 检查错误
    
    not enough values to unpack (expected 2, got 1)
    None" while parsing file:/e:/workdata/pycharmprojects/odoo-12/odoo/myaddons/business_entertainment/views/business_view.xml:4, near
    <record id="view_tree_business" model="ir.ui.view">
    

    是不是不能用等号呀,一个用户可能兼有多个权限组


  • 管理员

    @万叶言雨字段domain过滤权限问题,请各位大大看看 中说:

    是不是不能用等号呀,一个用户可能兼有多个权限组

    你说的对,domain改成:

    user = fields.Many2one('res.users', domain=lambda self: [('groups_id', 'in', self.env.ref('权限组xmlid').ids)])
    

    你上面的错误应该是xmlid错了,xmlid必须是模块名.xmlid,即使在本模块中定义的xmlid也要带上本模块的名字。

    另外,之所以这里要将domain定义在python文件的field上的原因是我们需要将xmlid解析为数据库id,在前端处理很麻烦,需要先eval这个ref(xxxx)


  • 管理员

    @万叶言雨字段domain过滤权限问题,请各位大大看看 中说:

    是不是不能用等号呀,一个用户可能兼有多个权限组

    你说的对,domain改成:

    user = fields.Many2one('res.users', domain=lambda self: [('groups_id', 'in', self.env.ref('权限组xmlid').ids)])
    

    你上面的错误应该是xmlid错了,xmlid必须是模块名.xmlid,即使在本模块中定义的xmlid也要带上本模块的名字。

    另外,之所以这里要将domain定义在python文件的field上的原因是我们需要将xmlid解析为数据库id,在前端处理很麻烦,需要先eval这个ref(xxxx)



  • @digitalsatori
    groups_id不需要加.id么。这个in是前面元素是否在后面元素列表里,我看后面只是单个权限组,前面groups_id是res_users模型的群组,应该是列表字段吧



  • @万叶言雨 加上模块名报这个,找不到id:

      self._write(store_vals)
      File "E:\workdata\PycharmProjects\odoo-12\odoo\models.py", line 3457, in _write
        self._validate_fields(vals)
      File "E:\workdata\PycharmProjects\odoo-12\odoo\models.py", line 1112, in _validate_fields
        raise ValidationError("%s\n\n%s" % (_("Error while validating constraint"), tools.ustr(e)))
    odoo.tools.convert.ParseError: "constraint 检查错误
    
    External ID not found in the system: business.entertainment.sheet.yz_group_bm
    None" while parsing file:/e:/workdata/pycharmprojects/odoo-12/odoo/myaddons/business_entertainment/views/business_view.xml:4, near
    <record id="view_tree_business" model="ir.ui.view">
    


  • @万叶言雨 ,就是最开始我写的domain没报错误,表单点击字段说是没有访问权限
    替代文字
    但用管理员登录是没问题的,也正常显示对应权限组下的用户。用其他用户登录就不行,然后不加domain过滤的话,所有用户登上去都是正常点击并能显示全部用户列表


  • 管理员

    你的报错,是view里的错误啊。business.entertainment.sheet.yz_group_bm这是什么?


  • 管理员

    @万叶言雨字段domain过滤权限问题,请各位大大看看 中说:

    @digitalsatori
    groups_id不需要加.id么。这个in是前面元素是否在后面元素列表里,我看后面只是单个权限组,前面groups_id是res_users模型的群组,应该是列表字段吧

    不需要。的确,真正的语意是倒过来才对,但是左侧必须是对象上的字段,所以Odoo会做这个反转的逻辑操作的。



  • @digitalsatori
    business.entertainment.sheet是模型_name,view里domain去掉了


  • 管理员

    嗯,不过从你view里报的错就知道你的xmlid写得还是不对,模块名.xmlidxxxxxxxx 不是 “模型名.xmlidxxxxxxx"



  • @digitalsatori 嗯嗯,可以了。刚看错了😥 ,非常感谢


  • 管理员

    不客气,很高兴对你有帮助。


Log in to reply