字段“ mobile_number”不存在(odoo12)

时间:2020-01-12 18:01:52

标签: python odoo

我想用手机号码为odoo创建一个注册模块,但是遇到以下错误:

Odoo Server Error
Traceback (most recent call last):
  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\models.py", line 1108, in _validate_fields
    check(self)
  File "c:\program files (x86)\odoo 12.0\server\odoo\addons\base\models\ir_ui_view.py", line 351, in _check_xml
    self.postprocess_and_fields(view.model, view_doc, view.id)
  File "c:\program files (x86)\odoo 12.0\server\odoo\addons\base\models\ir_ui_view.py", line 1111, in postprocess_and_fields
    self.raise_view_error(message, view_id)
  File "c:\program files (x86)\odoo 12.0\server\odoo\addons\base\models\ir_ui_view.py", line 548, in raise_view_error
    raise ValueError(message)
ValueError: Field `mobile_number` does not exist

Error context:
View `view.users.form.inherit`
[view_id: 859, xml_id: tg_signup_mobile.view_users_form_inherit, model: res.users, parent_id: 144]

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\tools\convert.py", line 758, in parse
    self._tags[rec.tag](rec, de, mode=mode)
  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\tools\convert.py", line 663, in _tag_record
    record = model.with_context(rec_context)._load_records([data], self.mode == 'update')
  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\models.py", line 3841, in _load_records
    data['record']._load_records_write(data['values'])
  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\models.py", line 3779, in _load_records_write
    self.write(values)
  File "c:\program files (x86)\odoo 12.0\server\odoo\addons\base\models\ir_ui_view.py", line 444, in write
    return super(View, self).write(self._compute_defaults(vals))
  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\models.py", line 3350, in write
    fields[0].determine_inverse(records)
  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\fields.py", line 1125, in determine_inverse
    getattr(records, self.inverse)()
  File "c:\program files (x86)\odoo 12.0\server\odoo\addons\base\models\ir_ui_view.py", line 275, in _inverse_arch
    view.write(data)
  File "c:\program files (x86)\odoo 12.0\server\odoo\addons\base\models\ir_ui_view.py", line 444, in write
    return super(View, self).write(self._compute_defaults(vals))
  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\models.py", line 3324, in write
    self._write(store_vals)
  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\models.py", line 3460, in _write
    self._validate_fields(vals)
  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\models.py", line 1112, in _validate_fields
    raise ValidationError("%s\n\n%s" % (_("Error while validating constraint"), tools.ustr(e)))
odoo.exceptions.ValidationError: ('Error while validating constraint\n\nField `mobile_number` does not exist\n\nError context:\nView `view.users.form.inherit`\n[view_id: 859, xml_id: tg_signup_mobile.view_users_form_inherit, model: res.users, parent_id: 144]', None)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\http.py", line 656, in _handle_exception
    return super(JsonRequest, self)._handle_exception(exception)
  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\http.py", line 314, in _handle_exception
    raise pycompat.reraise(type(exception), exception, sys.exc_info()[2])
  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\tools\pycompat.py", line 87, in reraise
    raise value
  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\http.py", line 698, in dispatch
    result = self._call_function(**self.params)
  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\http.py", line 346, in _call_function
    return checked_call(self.db, *args, **kwargs)
  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\service\model.py", line 97, in wrapper
    return f(dbname, *args, **kwargs)
  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\http.py", line 339, in checked_call
    result = self.endpoint(*a, **kw)
  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\http.py", line 941, in __call__
    return self.method(*args, **kw)
  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\http.py", line 519, in response_wrap
    response = f(*args, **kw)
  File "c:\program files (x86)\odoo 12.0\server\odoo\addons\web\controllers\main.py", line 966, in call_button
    action = self._call_kw(model, method, args, {})
  File "c:\program files (x86)\odoo 12.0\server\odoo\addons\web\controllers\main.py", line 954, in _call_kw
    return call_kw(request.env[model], method, args, kwargs)
  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\api.py", line 759, in call_kw
    return _call_kw_multi(method, model, args, kwargs)
  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\api.py", line 746, in _call_kw_multi
    result = method(recs, *args, **kwargs)
  File "<decorator-gen-67>", line 2, in button_immediate_upgrade
  File "c:\program files (x86)\odoo 12.0\server\odoo\addons\base\models\ir_module.py", line 74, in check_and_log
    return method(self, *args, **kwargs)
  File "c:\program files (x86)\odoo 12.0\server\odoo\addons\base\models\ir_module.py", line 622, in button_immediate_upgrade
    return self._button_immediate_function(type(self).button_upgrade)
  File "c:\program files (x86)\odoo 12.0\server\odoo\addons\base\models\ir_module.py", line 561, in _button_immediate_function
    modules.registry.Registry.new(self._cr.dbname, update_module=True)
  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\modules\registry.py", line 86, in new
    odoo.modules.load_modules(registry._db, force_demo, status, update_module)
  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\modules\loading.py", line 417, in load_modules
    force, status, report, loaded_modules, update_module, models_to_check)
  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\modules\loading.py", line 313, in load_marked_modules
    perform_checks=perform_checks, models_to_check=models_to_check
  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\modules\loading.py", line 222, in load_module_graph
    load_data(cr, idref, mode, kind='data', package=package, report=report)
  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\modules\loading.py", line 68, in load_data
    tools.convert_file(cr, package.name, filename, idref, mode, noupdate, kind, report)
  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\tools\convert.py", line 802, in convert_file
    convert_xml_import(cr, module, fp, idref, mode, noupdate, report)
  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\tools\convert.py", line 865, in convert_xml_import
    obj.parse(doc.getroot(), mode=mode)
  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\tools\convert.py", line 755, in parse
    self.parse(rec, mode)
  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\tools\convert.py", line 764, in parse
    exc_info[2]
  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\tools\pycompat.py", line 86, in reraise
    raise value.with_traceback(tb)
  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\tools\convert.py", line 758, in parse
    self._tags[rec.tag](rec, de, mode=mode)
  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\tools\convert.py", line 663, in _tag_record
    record = model.with_context(rec_context)._load_records([data], self.mode == 'update')
  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\models.py", line 3841, in _load_records
    data['record']._load_records_write(data['values'])
  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\models.py", line 3779, in _load_records_write
    self.write(values)
  File "c:\program files (x86)\odoo 12.0\server\odoo\addons\base\models\ir_ui_view.py", line 444, in write
    return super(View, self).write(self._compute_defaults(vals))
  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\models.py", line 3350, in write
    fields[0].determine_inverse(records)
  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\fields.py", line 1125, in determine_inverse
    getattr(records, self.inverse)()
  File "c:\program files (x86)\odoo 12.0\server\odoo\addons\base\models\ir_ui_view.py", line 275, in _inverse_arch
    view.write(data)
  File "c:\program files (x86)\odoo 12.0\server\odoo\addons\base\models\ir_ui_view.py", line 444, in write
    return super(View, self).write(self._compute_defaults(vals))
  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\models.py", line 3324, in write
    self._write(store_vals)
  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\models.py", line 3460, in _write
    self._validate_fields(vals)
  File "C:\Program Files (x86)\Odoo 12.0\server\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: "Error while validating constraint

Field `mobile_number` does not exist

Error context:
View `view.users.form.inherit`
[view_id: 859, xml_id: tg_signup_mobile.view_users_form_inherit, model: res.users, parent_id: 144]
None" while parsing file:/c:/program%20files%20(x86)/odoo%2012.0/server/local_addons/tg_signup_mobile/views/view_users_form.xml:4, near
<record id="view_users_form_inherit" model="ir.ui.view">
            <field name="name">view.users.form.inherit</field>
            <field name="model">res.users</field>
            <field name="inherit_id" ref="base.view_users_form"/>
            <field name="arch" type="xml">
                <xpath expr="//h2/field[@name='login']" position="after">
                    <h2><field name="mobile_number"/></h2>
                </xpath>
            </field>
        </record>

我的模特:

from re import search
from odoo import models, fields, api
from odoo.exceptions import ValidationError

class ResUsers(models.Model):
    _inherit = "res.users"
    mobile_number = fields.Char(string="شماره موبایل", required=True, size=11)

    @api.constrains("mobile_number")
    def check_validation(self):
        for record in self:
            if record.mobile_number:
                if not search("^(\+98|0)?9\d{9}$", record.mobile_number):
                    raise ValidationError("شماره موبایل وارد شده نامعتبر است.")

我的观点:

<?xml version="1.0" encoding="UTF-8"?>
<odoo>
    <data>
         <record id="view_users_form_inherit" model="ir.ui.view">
            <field name="name">view.users.form.inherit</field>
            <field name="model">res.users</field>
            <field name="inherit_id" ref="base.view_users_form"/>
            <field name="arch" type="xml">
                <xpath expr="//h2/field[@name='login']" position="after">
                    <h2><field name="mobile_number"/></h2>
                </xpath>
            </field>
        </record>
    </data>
</odoo>

我依靠基本模块来提高效率。我正在odoo 12中运行。

我得到错误的原因是什么?

1 个答案:

答案 0 :(得分:1)

我自己发现了问题。除了基本模块外,我还必须包括base_setup模块。