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()