Search code examples
pythonodooodoo-8

Filter with a computed field is not working [Odoo 8]


I have to display in a tree view the number of attachments an invoice has. This is OK, I'm doing it with a compute.

I also have to be able to filter the invoices that have attached files and here is the problem. I can't use this field in searching because it doesn't have store=True attribute, and if I add it, the compute attribute stops working

I was investigating and there is a search=" " attribute that can be put on the field I have it this way right now, but for some reason it's not working for me.

attachment_count = fields.Integer(string="QAF", compute='count_attachments', search='_search_att_count')

    @api.one
    def count_attachments(self):
        # code

    def _search_att_count(self, operator, value):
        field_id = self.search([])
        if operator == '=':
            field_id = [x.id for x in field_id if x.attachment_count == value]
        return [('id', 'in', field_id)]

I'm getting this error:

 Traceback (most recent call last):   File
 "/opt/odoo/odoo/openerp/http.py", line 546, in _handle_exception
     return super(JsonRequest, self)._handle_exception(exception)   File "/opt/odoo/odoo/openerp/http.py", line 583, in dispatch
     result = self._call_function(**self.params)   File "/opt/odoo/odoo/openerp/http.py", line 319, in _call_function
     return checked_call(self.db, *args, **kwargs)   File "/opt/odoo/odoo/openerp/service/model.py", line 118, in wrapper
     return f(dbname, *args, **kwargs)   File "/opt/odoo/odoo/openerp/http.py", line 316, in checked_call
     return self.endpoint(*a, **kw)   File "/opt/odoo/odoo/openerp/http.py", line 812, in __call__
     return self.method(*args, **kw)   File "/opt/odoo/odoo/openerp/http.py", line 412, in response_wrap
     response = f(*args, **kw)   File "/opt/odoo/odoo/addons/web/controllers/main.py", line 884, in
 search_read
     return self.do_search_read(model, fields, offset, limit, domain, sort)   File "/opt/odoo/odoo/addons/web/controllers/main.py", line
 905, in do_search_read
     request.context)   File "/opt/odoo/odoo/openerp/http.py", line 917, in proxy
     result = meth(cr, request.uid, *args, **kw)   File "/opt/odoo/odoo/openerp/api.py", line 268, in wrapper
     return old_api(self, *args, **kwargs)   File "/opt/odoo/odoo/openerp/models.py", line 5184, in search_read
     record_ids = self.search(cr, uid, domain or [], offset=offset, limit=limit, order=order, context=context)   File
 "/opt/odoo/odoo/openerp/api.py", line 268, in wrapper
     return old_api(self, *args, **kwargs)   File "/opt/odoo/odoo/openerp/models.py", line 1650, in search
     return self._search(cr, user, args, offset=offset, limit=limit, order=order, context=context, count=count)   File
 "/opt/odoo/odoo/openerp/api.py", line 268, in wrapper
     return old_api(self, *args, **kwargs)   File "/opt/odoo/odoo/openerp/models.py", line 4687, in _search
     query = self._where_calc(cr, user, args, context=context)   File "/opt/odoo/odoo/openerp/api.py", line 268, in wrapper
     return old_api(self, *args, **kwargs)   File "/opt/odoo/odoo/openerp/models.py", line 4500, in _where_calc
     where_clause, where_params = e.to_sql()   File "/opt/odoo/odoo/openerp/osv/expression.py", line 1287, in to_sql
     q, p = self.__leaf_to_sql(leaf)   File "/opt/odoo/odoo/openerp/osv/expression.py", line 1154, in
 __leaf_to_sql
 Blockquote
     "Invalid value %r in domain term %r" % (right, leaf) AssertionError: Invalid value account.invoice(29521, 24984, 16542,
 23652, 23651, 28875, 36436, 9637, 24800

And the number continous


Solution

  • self.search([]) returns a list of recordsets. So when your code doesn't match the "if" condition, it will throw an error.

    You need to add an "else" condition to get a list of record ids. For example:

    def _search_att_count(self, operator, value):
        field_id = self.search([])
        if operator == '=':
            field_id = [x.id for x in field_id if x.attachment_count == value]
        else:
            field_id = field_id.ids
        return [('id', 'in', field_id)]