Skip to Content
Menu
This question has been flagged

Hi,

I am working on migrating from odoo 12 to 14, and I am having some problems with the inheritance. My files are as following:

models:

from odoo import models, fields, api, _

class StockLocation(models.Model):
_inherit = "stock.location"

is_empty = fields.Boolean(String="Empty Location", compute="generate_empty_locations")

def generate_empty_locations(self):
query = """SELECT CAST(row_number() OVER () as integer) AS id, t2.location_id AS x_bve_location_id, t1.complete_name AS x_bve_t1_display_name,
t2.quantity AS x_bve_t2_quantity FROM stock_location AS t1
LEFT JOIN stock_quant AS t2 ON t2.location_id = t1.id
WHERE t2.quantity is null and t1.active = true
and (t1.company_id = 1 or t1.company_id = 2 or t1.company_id is null)
and t1.usage = 'internal';
"""
self.env.cr.execute(query)
query_results = self.env.cr.dictfetchall()
for rec in query_results:
exist = self.env['stock.location'].search_read([('complete_name', '=', rec.get('x_bve_t1_display_name'))])
for e in exist:
loca_id = e['id']
self.env['stock.location'].browse(loca_id).is_empty = True


views:

<?xml version="1.0" encoding="utf-8"?>
<odoo>
<record id="view_empty_location_report" model="ir.ui.view">
<field name="name">Empty Location Report</field>
<field name="model">stock.location</field>
<field name="arch" type="xml">
<tree string="Empty Location Report" decoration-bf="not is_empty" create="0" >
<field name="display_name"/>
<field name="is_empty" />
</tree>
</field>
</record>

<record id="view_empty_location_filter" model="ir.ui.view">
<field name="name">stock.location.empty.filter</field>
<field name="model">stock.location</field>
<field name="inherit_id" ref="stock.view_location_search" />
<field name="arch" type="xml">
<xpath expr="//search" position="inside">
<filter name="empty" string="Empty" domain="[('is_empty', '=', True)]"/>
</xpath>
</field>
</record>

<record id="action_report_empty_location" model="ir.actions.server">
<field name="name">Report Empty Location</field>
<field name="model_id" ref="model_stock_location"/>
<field name="binding_model_id" ref="stock.model_stock_location"/>
<field name="state">code</field>
<field name="code">
if records:
action = records.generate_empty_locations()
</field>
</record>

<record id="action_empty_location_report" model="ir.actions.act_window">
<field name="name">Empty Location Report</field>
<field name="res_model">stock.location</field>
<field name="binding_view_types">form</field>
<field name="view_mode">tree</field>
<field name="view_id" ref="view_empty_location_report"/>
<field name="target">current</field>
<field name="domain">[('usage','=','internal')]</field>

<!-- <field name="context">{'default_compute_at_date': 0}</field> -->
</record>

<menuitem id="menu_empty_location_report" name="Empty Location Report" parent="stock.menu_warehouse_report" sequence="100" action="action_empty_location_report"/>
</odoo>


and error:

Error:

Odoo Server Error


Traceback (most recent call last):

  File "/home/xiao/projects/odoo14/odoo/tools/convert.py", line 677, in _tag_root

    f(rec)

  File "/home/xiao/projects/odoo14/odoo/tools/convert.py", line 580, in _tag_record

    record = model._load_records([data], self.mode == 'update')

  File "/home/xiao/projects/odoo14/odoo/models.py", line 4193, in _load_records

    data['record']._load_records_write(data['values'])

  File "/home/xiao/projects/odoo14/odoo/addons/base/models/ir_ui_view.py", line 1836, in _load_records_write

    super(View, self)._load_records_write(values)

  File "/home/xiao/projects/odoo14/odoo/models.py", line 4130, in _load_records_write

    self.write(values)

  File "/home/xiao/projects/odoo14/odoo/addons/base/models/ir_ui_view.py", line 500, in write

    res = super(View, self).write(self._compute_defaults(vals))

  File "/home/xiao/projects/odoo14/odoo/models.py", line 3688, in write

    fields[0].determine_inverse(real_recs)

  File "/home/xiao/projects/odoo14/odoo/fields.py", line 1152, in determine_inverse

    getattr(records, self.inverse)()

  File "/home/xiao/projects/odoo14/odoo/addons/base/models/ir_ui_view.py", line 300, in _inverse_arch

    view.write(data)

  File "/home/xiao/projects/odoo14/odoo/addons/base/models/ir_ui_view.py", line 500, in write

    res = super(View, self).write(self._compute_defaults(vals))

  File "/home/xiao/projects/odoo14/odoo/models.py", line 3683, in write

    real_recs._validate_fields(set(vals) - set(inverse_fields))

  File "/home/xiao/projects/odoo14/odoo/models.py", line 1251, in _validate_fields

    check(self)

  File "/home/xiao/projects/odoo14/odoo/addons/base/models/ir_ui_view.py", line 411, in _check_xml

    )).with_traceback(e.__traceback__) from None

  File "/home/xiao/projects/odoo14/odoo/addons/base/models/ir_ui_view.py", line 391, in _check_xml

    view.postprocess_and_fields(view_doc, validate=True)

  File "/home/xiao/projects/odoo14/odoo/addons/base/models/ir_ui_view.py", line 851, in postprocess_and_fields

    arch, name_manager = self._postprocess_view(node, model, validate=validate)

  File "/home/xiao/projects/odoo14/odoo/addons/base/models/ir_ui_view.py", line 863, in _postprocess_view

    self.postprocess(node, [], editable, name_manager)

  File "/home/xiao/projects/odoo14/odoo/addons/base/models/ir_ui_view.py", line 963, in postprocess

    self.postprocess(child, current_node_path, node_info['editable'], name_manager)

  File "/home/xiao/projects/odoo14/odoo/addons/base/models/ir_ui_view.py", line 955, in postprocess

    self._validate_attrs(node, name_manager, node_info)

  File "/home/xiao/projects/odoo14/odoo/addons/base/models/ir_ui_view.py", line 1294, in _validate_attrs

    fields = self._get_server_domain_variables(expr, 'domain of <%s%s> ' % (node.tag, (' name="%s"' % node.get('name')) if node.get('name') else '' ), Model)

  File "/home/xiao/projects/odoo14/odoo/addons/base/models/ir_ui_view.py", line 1533, in _get_server_domain_variables

    self.handle_view_error(msg)

  File "/home/xiao/projects/odoo14/odoo/addons/base/models/ir_ui_view.py", line 673, in handle_view_error

    raise ValueError(formatted_message).with_traceback(from_traceback) from from_exception

odoo.exceptions.ValidationError: Error while validating view:


Unsearchable field "stock.location.is_empty" in path 'is_empty' in domain of <filter name="empty"> ="[('is_empty', '=', True)]"


View name: stock.location.empty.filter

Error context:

 view: ir.ui.view(1542,)

 xmlid: view_empty_location_filter

 view.model: stock.location

 view.parent: ir.ui.view(1006,)

 file: /home/xiao/projects/odoo14/devoca-odoo-addons/devoca_custom_addons/views/empty_location_report.xml



The above exception was the direct cause of the following exception:


Traceback (most recent call last):

  File "/home/xiao/projects/odoo14/odoo/addons/base/models/ir_http.py", line 237, in _dispatch

    result = request.dispatch()

  File "/home/xiao/projects/odoo14/odoo/http.py", line 683, in dispatch

    result = self._call_function(**self.params)

  File "/home/xiao/projects/odoo14/odoo/http.py", line 359, in _call_function

    return checked_call(self.db, *args, **kwargs)

  File "/home/xiao/projects/odoo14/odoo/service/model.py", line 94, in wrapper

    return f(dbname, *args, **kwargs)

  File "/home/xiao/projects/odoo14/odoo/http.py", line 347, in checked_call

    result = self.endpoint(*a, **kw)

  File "/home/xiao/projects/odoo14/odoo/http.py", line 912, in __call__

    return self.method(*args, **kw)

  File "/home/xiao/projects/odoo14/odoo/http.py", line 531, in response_wrap

    response = f(*args, **kw)

  File "/home/xiao/projects/odoo14/addons/web/controllers/main.py", line 1381, in call_button

    action = self._call_kw(model, method, args, kwargs)

  File "/home/xiao/projects/odoo14/addons/web/controllers/main.py", line 1369, in _call_kw

    return call_kw(request.env[model], method, args, kwargs)

  File "/home/xiao/projects/odoo14/odoo/api.py", line 396, in call_kw

    result = _call_kw_multi(method, model, args, kwargs)

  File "/home/xiao/projects/odoo14/odoo/api.py", line 383, in _call_kw_multi

    result = method(recs, *args, **kwargs)

  File "<decorator-gen-78>", line 2, in button_immediate_upgrade

  File "/home/xiao/projects/odoo14/odoo/addons/base/models/ir_module.py", line 73, in check_and_log

    return method(self, *args, **kwargs)

  File "/home/xiao/projects/odoo14/odoo/addons/base/models/ir_module.py", line 653, in button_immediate_upgrade

    return self._button_immediate_function(type(self).button_upgrade)

  File "/home/xiao/projects/odoo14/odoo/addons/base/models/ir_module.py", line 592, in _button_immediate_function

    modules.registry.Registry.new(self._cr.dbname, update_module=True)

  File "/home/xiao/projects/odoo14/odoo/modules/registry.py", line 89, in new

    odoo.modules.load_modules(registry._db, force_demo, status, update_module)

  File "/home/xiao/projects/odoo14/odoo/modules/loading.py", line 451, in load_modules

    force, status, report, loaded_modules, update_module, models_to_check)

  File "/home/xiao/projects/odoo14/odoo/modules/loading.py", line 348, in load_marked_modules

    perform_checks=perform_checks, models_to_check=models_to_check

  File "/home/xiao/projects/odoo14/odoo/modules/loading.py", line 221, in load_module_graph

    load_data(cr, idref, mode, kind='data', package=package)

  File "/home/xiao/projects/odoo14/odoo/modules/loading.py", line 69, in load_data

    tools.convert_file(cr, package.name, filename, idref, mode, noupdate, kind)

  File "/home/xiao/projects/odoo14/odoo/tools/convert.py", line 733, in convert_file

    convert_xml_import(cr, module, fp, idref, mode, noupdate)

  File "/home/xiao/projects/odoo14/odoo/tools/convert.py", line 799, in convert_xml_import

    obj.parse(doc.getroot())

  File "/home/xiao/projects/odoo14/odoo/tools/convert.py", line 719, in parse

    self._tag_root(de)

  File "/home/xiao/projects/odoo14/odoo/tools/convert.py", line 685, in _tag_root

    )) from e

Exception


The above exception was the direct cause of the following exception:


Traceback (most recent call last):

  File "/home/xiao/projects/odoo14/odoo/http.py", line 639, in _handle_exception

    return super(JsonRequest, self)._handle_exception(exception)

  File "/home/xiao/projects/odoo14/odoo/http.py", line 315, in _handle_exception

    raise exception.with_traceback(None) from new_cause

odoo.tools.convert.ParseError: while parsing /home/xiao/projects/odoo14/devoca-odoo-addons/devoca_custom_addons/views/empty_location_report.xml:14, near

<record id="view_empty_location_filter" model="ir.ui.view">

        <field name="name">stock.location.empty.filter</field>        

        <field name="model">stock.location</field>

        <field name="inherit_id" ref="stock.view_location_search"/>

        <field name="arch" type="xml">

            <xpath expr="//search" position="inside">

                <filter name="empty" string="Empty" domain="[('is_empty', '=', True)]"/>

                

            </xpath>

        </field>

    </record>


I am guessing the error must be related to the attribute is_empty when passing it to filter domain. I would really appreciate any helps, and many thanks in advance. 


Avatar
Discard
Best Answer

Hello Daniel,

Your problem is by default computed fields are not stored in the database (reference), and you cannot filter by a field unless it is stored, so if you change the is_empty field to:

is_empty = fields.Boolean(String="Empty Location", compute="generate_empty_locations", store=True)

It should enable the field to be searchable. I have tested this and is working well for me. 

I hope this helps, 

Avatar
Discard
Related Posts Replies Views Activity
1
Sep 23
1081
1
Jun 22
4908
7
Jun 24
21704
1
Sep 22
398
2
Jan 22
1908