Odoo 12上不存在字段

时间:2019-08-01 22:34:13

标签: odoo odoo-12

我试图在Odoo 12中制作一个模块,但一直给我一个错误字段,但我已经在模型内部定义了该字段

已经检查过几次并更改了一些代码,但还是没有运气

model.py

class ProductProduct(models.Model):
    _inherit = 'product.product'

    isbn = fields.Char('ISBN Code', unique=True, help="Shows International Standard Book Number")
    catalog_num = fields.Char('Catalog Number', help="Shows Identification Number of Books")
    lang = fields.Selection(string='Language', selection='_get_lang')
    author_id = fields.Many2one('res.partner','Author',domain=[('penulis','=',True)])
    publisher_id = fields.Many2one('res.partner','Publisher',domain=[('penerbit','=',True)])
    nbpage = fields.Integer('Number of Pages')
    location_id = fields.Many2one('stock.location', 'Location', help="Shows position of book", domain=[('lokasi buku','=',True)])
    num_edition = fields.Integer('No. Edition', help="Edition number of book")
    resensi = fields.Text('Resensi')
    state = fields.Selection([('available','Available'),('rent','Rented')],'State',readonly=True, default='available')

    _sql_constraints = [
        ('unique_barcode','unique(barcode)', 'barcode field must be unique across all the products'),
        ('code_uniq', 'unique (default_code', 'Code of the product must be unique!')
    ]

    @api.model
    def _get_lang(self):
        return self.env['res.lang'].get_installed()

view.xml:

    <record model="ir.ui.view" id="buku_tree_view">
        <field name="name">buku.tree</field>
        <field name="model">product.product</field>
        <field name="arch" type="xml">
            <tree string="Buku">
                <field name="default_code"/>
                <field name="name"/>
                <field name="publisher_id"/>
                <field name="author_id"/>
                <field name="location_id"/>
                <field name="categ_id"/>
                <field name="state"/>
            </tree>
        </field>
    </record>

我不断收到“字段publisher_id不存在”的信息,但是正如您所看到的,我已经在model.py中定义了它。

错误消息:

Odoo Server Error
Traceback (most recent call last):
  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\models.py", line 1100, in _validate_fields
    check(self)
  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\addons\base\models\ir_ui_view.py", line 344, 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 1085, 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 538, in raise_view_error
    raise ValueError(message)
ValueError: Field `publisher_id` does not exist

Error context:
View `buku.tree`
[view_id: 531, xml_id: n/a, model: product.product, parent_id: n/a]

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 748, in parse
    self._tags[rec.tag](rec, de, mode=mode)
  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\tools\convert.py", line 653, 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 3795, in _load_records
    records = self.create([data['values'] for data in to_create])
  File "<decorator-gen-32>", line 2, in create
  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\api.py", line 452, in _model_create_multi
    return create(self, arg)
  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\addons\base\models\ir_ui_view.py", line 418, in create
    return super(View, self).create(vals_list)
  File "<decorator-gen-3>", line 2, in create
  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\api.py", line 452, in _model_create_multi
    return create(self, arg)
  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\models.py", line 3540, in create
    fields[0].determine_inverse(batch_recs)
  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\fields.py", line 1104, 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 268, in _inverse_arch
    view.write(data)
  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\addons\base\models\ir_ui_view.py", line 434, in write
    return super(View, self).write(self._compute_defaults(vals))
  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\models.py", line 3257, in write
    self._write(store_vals)
  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\models.py", line 3401, in _write
    self._validate_fields(vals)
  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\models.py", line 1104, 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 `publisher_id` does not exist\n\nError context:\nView `buku.tree`\n[view_id: 531, xml_id: n/a, model: product.product, parent_id: n/a]', 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 653, in _handle_exception
    return super(JsonRequest, self)._handle_exception(exception)
  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\http.py", line 312, 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 695, in dispatch
    result = self._call_function(**self.params)
  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\http.py", line 344, 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 337, in checked_call
    result = self.endpoint(*a, **kw)
  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\http.py", line 938, in __call__
    return self.method(*args, **kw)
  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\http.py", line 517, 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 749, in call_kw
    return _call_kw_multi(method, model, args, kwargs)
  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\api.py", line 736, in _call_kw_multi
    result = method(recs, *args, **kwargs)
  File "<decorator-gen-61>", line 2, in button_immediate_install
  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\addons\base\models\ir_module.py", line 71, 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 442, in button_immediate_install
    return self._button_immediate_function(type(self).button_install)
  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\addons\base\models\ir_module.py", line 535, 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 426, in load_modules
    loaded_modules, update_module, models_to_check)
  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\modules\loading.py", line 318, 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 224, 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 792, 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 853, in convert_xml_import
    obj.parse(doc.getroot(), mode=mode)
  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\tools\convert.py", line 745, in parse
    self.parse(rec, mode)
  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\tools\convert.py", line 754, 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 748, in parse
    self._tags[rec.tag](rec, de, mode=mode)
  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\tools\convert.py", line 653, 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 3795, in _load_records
    records = self.create([data['values'] for data in to_create])
  File "<decorator-gen-32>", line 2, in create
  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\api.py", line 452, in _model_create_multi
    return create(self, arg)
  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\addons\base\models\ir_ui_view.py", line 418, in create
    return super(View, self).create(vals_list)
  File "<decorator-gen-3>", line 2, in create
  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\api.py", line 452, in _model_create_multi
    return create(self, arg)
  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\models.py", line 3540, in create
    fields[0].determine_inverse(batch_recs)
  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\fields.py", line 1104, 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 268, in _inverse_arch
    view.write(data)
  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\addons\base\models\ir_ui_view.py", line 434, in write
    return super(View, self).write(self._compute_defaults(vals))
  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\models.py", line 3257, in write
    self._write(store_vals)
  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\models.py", line 3401, in _write
    self._validate_fields(vals)
  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\models.py", line 1104, 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 `publisher_id` does not exist

Error context:
View `buku.tree`
[view_id: 531, xml_id: n/a, model: product.product, parent_id: n/a]
None" while parsing file:/c:/program%20files%20(x86)/odoo%2012.0/server/odoo/addons/aa_library/views/views.xml:18, near
<record model="ir.ui.view" id="buku_tree_view">
        <field name="name">buku.tree</field>
        <field name="model">product.product</field>
        <field name="arch" type="xml">
            <tree string="Buku">
                <field name="default_code"/>
                <field name="name"/>
                <field name="publisher_id"/>
                <field name="author_id"/>
                <field name="location_id"/>
                <field name="categ_id"/>
                <field name="state"/>
            </tree>
        </field>
    </record>

有人知道我的代码有什么问题吗?

3 个答案:

答案 0 :(得分:0)

如果您一次在模型(.py文件)和视图中添加了该字段,则会收到此错误。尝试在视图中注释该字段,然后再次运行,您可以在视图中取消注释。

答案 1 :(得分:0)

您的代码完全正确,问题可能出在您将 .py文件添加到 init .py文件中。

  1. 将model.py文件导入 init .py文件
  2. 由于发生xml错误,odoo无法接受此更新,因此请在部分数据下的清单 .py中注释xml文件。
  3. 现在重新启动更新,现在取消注释xml重新启动,升级它应该可以了。

答案 2 :(得分:0)

我发现必须在安装模块之前安装“库存”模块。谢谢大家。