odoo16 AttributeError: type object ‘BaseModel‘ has no attribute ‘fields_view_get‘

Odoo16中,fields_view_get方法已被重命名为get_views,以提高性能并实现视图数据的缓存。在升级到Odoo16时,需要将旧的fields_view_get调用替换为get_views。文章讨论了这个更改以及get_views方法在处理视图数据和性能优化方面的作用。
摘要由CSDN通过智能技术生成

odoo16  AttributeError: type object 'BaseModel' has no attribute 'fields_view_get'

Odoo 15模块使用了很多方法fields_views_get来注入域和上下文,渲染组件,例如:动态调整domian、动态增加field字段的显示、修改field字段的属性等

1、动态增加或调整domain

2、动态增加field字段展示

3、修改field字段的属性

@api.model
    def fields_view_get(self, view_id=None, view_type='form', toolbar=False, submenu=False):
        """ fields_view_get([view_id | view_type='form'])

        Get the detailed composition of the requested view like fields, model, view architecture

        :param int view_id: id of the view or None
        :param str view_type: type of the view to return if view_id is None ('form', 'tree', ...)
        :param bool toolbar: true to include contextual actions
        :param submenu: deprecated
        :return: composition of the requested view (including inherited views and extensions)
        :rtype: dict
        :raise AttributeError:
                * if the inherited view has unknown position to work with other than 'before', 'after', 'inside', 'replace'
                * if some tag other than 'position' is found in parent view
        :raise Invalid ArchitectureError: if there is view type other than form, tree, calendar, search etc defined on the structure
        """
        self.check_access_rights('read')
        view = self.env['ir.ui.view'].sudo().browse(view_id)

        # Get the view arch and all other attributes describing the composition of the view
        result = self._fields_view_get(view_id=view_id, view_type=view_type, toolbar=toolbar, submenu=submenu)

        # Override context for postprocessing
        if view_id and result.get('base_model', self._name) != self._name:
            view = view.with_context(base_model_name=result['base_model'])

        # Apply post processing, groups and modifiers etc...
        xarch, xfields = view.postprocess_and_fields(etree.fromstring(result['arch']), model=self._name)
        result['arch'] = xarch
        result['fields'] = xfields

        # Add related action information if asked
        if toolbar:
            vt = 'list' if view_type == 'tree' else view_type
            bindings = self.env['ir.actions.actions'].get_bindings(self._name)
            resreport = [action
                         for action in bindings['report']
                         if vt in (action.get('binding_view_types') or vt).split(',')]
            resaction = [action
                         for action in bindings['action']
                         if vt in (action.get('binding_view_types') or vt).split(',')]

            result['toolbar'] = {
                'print': resreport,
                'action': resaction,
            }
        return result

网查下:odoo16目前fields_view_get已重命名为get_views,缓存视图数据,提升性能,fields_view_get() 改为 get_views() ,同时使用缓存对视图数据进行保存。

@api.model
    def fields_get_keys(self):
        warnings.warn(
            'fields_get_keys() method is deprecated, use `_fields` or `get_views` instead',
            DeprecationWarning, stacklevel=2,
        )
        return list(self._fields)
    @api.model
    def fields_get_keys(self):
        warnings.warn(
            'fields_get_keys() method is deprecated, use `_fields` or `get_views` instead',
            DeprecationWarning, stacklevel=2,
        )
        return list(self._fields)

在odoo16原码中查找get_views()经查找发现:

class Module(models.Model):
    _name = "ir.module.module"
    _rec_name = "shortdesc"
    _rec_names_search = ['name', 'shortdesc', 'summary']
    _description = "Module"
    _order = 'application desc,sequence,name'

    @api.model
    def get_views(self, views, options=None):
        res = super().get_views(views, options)
        if res['views'].get('form', {}).get('toolbar'):
            install_id = self.env.ref('base.action_server_module_immediate_install').id
            action = [rec for rec in res['views']['form']['toolbar']['action'] if rec.get('id', False) != install_id]
            res['views']['form']['toolbar'] = {'action': action}
        return res

    @classmethod
    def get_module_info(cls, name):
        try:
            return modules.get_manifest(name)
        except Exception:
            _logger.debug('Error when trying to fetch information for module %s', name, exc_info=True)
            return {}

原先写的代码,要进行转换,现在升级16后,不转换即可使用了。

# from odoo import http
# from odoo.http import request, Response, JsonRequest
# from odoo.tools import date_utils
#
#
# class JsonRequestNew(JsonRequest):
#
#     def _json_response(self, result=None, error=None):
#
#         # response = {
#         #    'jsonrpc': '2.0',
#         #    'id': self.jsonrequest.get('id')
#         #    }
#         # if error is not None:
#         #    response['error'] = error
#         # if result is not None:
#         #    response['result'] = result
#
#         responseData = super(JsonRequestNew, self)._json_response(result=result, error=error)
#
#         response = {}
#         if error is not None:
#             response = error
#         if result is not None:
#             response = result
#
#         mime = 'application/json'
#         body = json.dumps(response, default=date_utils.json_default)
#
#         return Response(
#             body, status=error and error.pop('http_status', 200) or 200,
#             headers=[('Content-Type', mime), ('Content-Length', len(body))]
#         )
#
#
# class RootNew(http.Root):
#
#     def get_request(self, httprequest):
#
#         # deduce type of request
#
#         jsonResponse = super(RootNew, self).get_request(httprequest=httprequest)
#
#         if httprequest.mimetype in ("application/json", "application/json-rpc"):
#             return JsonRequestNew(httprequest)
#         else:
#             return jsonResponse
#
#
# http.root = RootNew()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

信息化未来

你的鼓励将是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值