新建模块时models不能加载如何解决
-
@digitalsatori 听从您的,在新手提问区重新发帖。
我是使用source\odoo-bin scaffold shouhou myaddons代码直接生成一个模块的模板的,在\myaddons\shouhou\models文件夹下默认生成了_init_.py文件的,models、manifest、view三个文件也是默认创建好的,只不过其中的默认代码被注释掉了。我在模板上修改的过程是修改好models文件与manifest文件,然后在浏览器中更新应用列表,安装售后模块。在这之后如果编辑view文件,想要创建视图,那么就会出现之前所述问题,服务端后台奔溃,循环报错,但是如果在这时把view文件恢复到初始模板(主要代码均被注释)的状态后,服务端报错就会立刻停止。
另外因为我是在windows环境下开发,使用的版本是Godoo10,个人暂时没有找到服务端的控制台在哪,望告知 -
问题已经在之前的回复里提到了。
你需要检查一下
\shouhou\__init__.py
中是否有import models
(这是导入models python包), 在\shouhou\models\__init__.py
文件中是否有import models
(这是导入models python模块)
我估计你没有这些import
, 这就导致你在models.py
所定义的实体类模型,没有在系统中生成,而你的view中又要引用这些模型,这样就出错了。如果你不知道如何命令行重新加载的话,可以新建一个数据库再试试。
-
@digitalsatori 在 新建模块时models不能加载如何解决 中说:
mode
\shouhou \_init_.py
# -*- coding: utf-8 -*- from . import controllers from . import models
\shouhou\models
# -*- coding: utf-8 -*- from . import models
两个导入模块均有的,或许是我的格式有问题?
-
@Siyuan
.\_init_.py# -*- coding: utf-8 -*- from . import controllers from . import models
.\_manifest_.py
{ 'name': "shouhou", 'summary': """ 售后模块""", 'description': """ 售后模块 """, 'website': "", # Categories can be used to filter modules in modules listing # Check https://github.com/odoo/odoo/blob/master/openerp/addons/base/module/module_data.xml # for the full list 'category': 'Uncategorized', 'version': '0.1', # any module necessary for this one to work correctly 'depends': ['base'], # always loaded 'data': [ # 'security/ir.model.access.csv', 'views/views.xml', 'views/templates.xml', ], # only loaded in demonstration mode 'demo': [ 'demo/demo.xml', ], }
\models\models.py
from odoo import models, fields, api class shouhoudan(models.Model): _name = 'shouhou.shouhoudan' name = fields.Char(string="报修人") days = fields.Integer(string="预计处理时长") startdate = fields.Date(string="报修日期日期") reason = fields.Text(string="售后事由")
\models\_init_.py
# -*- coding: utf-8 -*- from . import models
\views\views.xml
<openerp> <data> <record id="view_tree_shouhou_shouhoudan" model="ir.ui.view"> <field name="name">售后单列表</field> <field name="model">shouhou.shouhoudan</field> <field name="arch" type="xml"> <tree> <field name="name"/> <field name="days"/> <field name="startdate"/> </tree> </field> </record> <record id="view_form_shouhou_shouhoudan" model="ir.ui.view"> <field name="name">售后单</field> <field name="model">shouhou.shouhoudan</field> <field name="arch" type="xml"> <form> <sheet> <group name="group_top" string="售后单"> <field name="name"/> <field name="days"/> <field name="startdate"/> <field name="reason"/> </group> </sheet> </form> </field> </record> <act_window id="action_shouhou_shouhoudan" name="售后单" res_model="shouhou.shouhoudan" view_mode="tree,form" /> <menuitem name="售后" id="menu_shouhou"/> <menuitem name="售后单" id="menu_shouhou_shouhoudan" parent="menu_shouhou" action="action_shouhou_shouhoudan"/> </data> </openerp>
-
@Siyuan
报错如下Traceback (most recent call last): File "D:\GOdoo10\source\odoo\http.py", line 638, in _handle_exception return super(JsonRequest, self)._handle_exception(exception) File "D:\GOdoo10\source\odoo\http.py", line 675, in dispatch result = self._call_function(**self.params) File "D:\GOdoo10\source\odoo\http.py", line 331, in _call_function return checked_call(self.db, *args, **kwargs) File "D:\GOdoo10\source\odoo\service\model.py", line 119, in wrapper return f(dbname, *args, **kwargs) File "D:\GOdoo10\source\odoo\http.py", line 324, in checked_call result = self.endpoint(*a, **kw) File "D:\GOdoo10\source\odoo\http.py", line 933, in __call__ return self.method(*args, **kw) File "D:\GOdoo10\source\odoo\http.py", line 504, in response_wrap response = f(*args, **kw) File "D:\GOdoo10\source\addons\web\controllers\main.py", line 866, in call_button action = self._call_kw(model, method, args, {}) File "D:\GOdoo10\source\addons\web\controllers\main.py", line 854, in _call_kw return call_kw(request.env[model], method, args, kwargs) File "D:\GOdoo10\source\odoo\api.py", line 681, in call_kw return call_kw_multi(method, model, args, kwargs) File "D:\GOdoo10\source\odoo\api.py", line 672, in call_kw_multi result = method(recs, *args, **kwargs) File "D:\GOdoo10\source\odoo\addons\base\module\module.py", line 527, in button_immediate_upgrade return self._button_immediate_function(type(self).button_upgrade) File "D:\GOdoo10\source\odoo\addons\base\module\module.py", line 484, in _button_immediate_function modules.registry.Registry.new(self._cr.dbname, update_module=True) File "D:\GOdoo10\source\odoo\modules\registry.py", line 78, in new odoo.modules.load_modules(registry._db, force_demo, status, update_module) File "D:\GOdoo10\source\odoo\modules\loading.py", line 333, in load_modules force, status, report, loaded_modules, update_module) File "D:\GOdoo10\source\odoo\modules\loading.py", line 235, in load_marked_modules loaded, processed = load_module_graph(cr, graph, progressdict, report=report, skip_modules=loaded_modules, perform_checks=perform_checks) File "D:\GOdoo10\source\odoo\modules\loading.py", line 156, in load_module_graph _load_data(cr, module_name, idref, mode, kind='data') File "D:\GOdoo10\source\odoo\modules\loading.py", line 95, in _load_data tools.convert_file(cr, module_name, filename, idref, mode, noupdate, kind, report) File "D:\GOdoo10\source\odoo\tools\convert.py", line 851, in convert_file convert_xml_import(cr, module, fp, idref, mode, noupdate, report) File "D:\GOdoo10\source\odoo\tools\convert.py", line 921, in convert_xml_import obj.parse(doc.getroot(), mode=mode) File "D:\GOdoo10\source\odoo\tools\convert.py", line 802, in parse self.parse(rec, mode) File "D:\GOdoo10\source\odoo\tools\convert.py", line 805, in parse self._tags[rec.tag](rec, de, mode=mode) File "D:\GOdoo10\source\odoo\tools\convert.py", line 443, in _tag_act_window id = self.env['ir.model.data']._update('ir.actions.act_window', self.module, res, xml_id, noupdate=self.isnoupdate(data_node), mode=self.mode) File "D:\GOdoo10\source\odoo\addons\base\ir\ir_model.py", line 1159, in _update record = record.create(values) File "D:\GOdoo10\source\odoo\addons\base\ir\ir_actions.py", line 340, in create return super(IrActionsActWindow, self).create(vals) File "D:\GOdoo10\source\odoo\addons\base\ir\ir_actions.py", line 40, in create res = super(IrActions, self).create(vals) File "D:\GOdoo10\source\odoo\models.py", line 3798, in create record = self.browse(self._create(old_vals)) File "D:\GOdoo10\source\odoo\models.py", line 3954, in _create self._validate_fields(vals) File "D:\GOdoo10\source\odoo\models.py", line 1058, in _validate_fields check(self) File "D:\GOdoo10\source\odoo\addons\base\ir\ir_actions.py", line 248, in _check_model raise ValidationError(_('Invalid model name %r in action definition.') % action.res_model) ParseError: "Invalid model name u'shouhou.shouhoudan' in action definition. None" while parsing file:///D:/GOdoo10/myaddons/shouhou/views/views.xml:38, near <act_window id="action_shouhou_shouhoudan" name="售后单" res_model="shouhou.shouhoudan" view_mode="tree,form"/>
应该就是模型未被成功的导入,但是却不知的为什么会导入不了
-
学习odoo模块创建,
推荐官方文档:
https://www.odoo.com/documentation/10.0/howtos/backend.html
Building a Module
-_-
-
你的模块有以下几个问题:
- .\init.py ,如果你的模块里面没有controllers,这行删掉。
- .\manifest.py,如果你的模块里面没有'views/templates.xml',这行删掉。
- .\manifest.py,如果你的模块里面没有'demo/demo.xml',,这行删掉。
- .\models\models.py,文件里面有中文字符,那就在首行加上编码,# -- coding: utf-8 --
然后重启odoo服务更新模块,如果还是出错。你可以在启动odoo server的时候通过命令行参数更新模块 -d 数据库名 -u模块名或者你可以删掉旧数据库再重新安装。