Odoo Help


filter column with domain and funtion

Francisco Castro
on 1/5/15, 11:44 AM 1,168 views

I want to filter a many2one field with a function like this (go to the account jounal and filter by type, this type depends on witch invoice I am customer or supplier, for example):

'journal_id': fields.many2one('account.journal', 'Refund Journal',domain=[('type','=',_get_journal)])

the function is this but I dont know if this is possible:

def _get_journal(self, cr, uid, context=None):

    #raise osv.except_osv(_('DEBUG'), _(str(context.keys())+str(context.values())))

        obj_journal = self.pool.get('account.journal')
        user_obj = self.pool.get('res.users')
        if context is None:
            context = {}
        inv_type = context.get('type', 'out_invoice')
        company_id = user_obj.browse(cr, uid, uid, context=context).company_id.id
    #raise osv.except_osv(_('DEBUG'), _(str(inv_type)))
    if inv_type == 'out_invoice' or inv_type == 'out_refund':
        type=(inv_type == 'out_invoice') and 'sale_refund' or \
               (inv_type == 'out_refund') and 'sale'
        #raise osv.except_osv(_('DEBUG'), _(type))
        type=(inv_type == 'in_invoice') and 'purchase_refund' or \
               (inv_type == 'in_refund') and 'purchase'

        #type = (inv_type == 'out_invoice') and 'sale_refund' or \
        #       (inv_type == 'out_refund') and 'sale' or \
        #       (inv_type == 'in_invoice') and 'purchase_refund' or \
        #       (inv_type == 'in_refund') and 'purchase'

        journal = obj_journal.search(cr, uid, [('type', '=', type), ('company_id','=',company_id)], limit=1, context=context)
        return str(journal) and str(journal[0]) or 'False'


this show an error:

TypeError: <function _get_journal at 0xb0529f0c> is not JSON serializable



Sudhir Arya (SA)

--Sudhir Arya (SA)--
| 6 8 8
Ahmedabad, India
--Sudhir Arya (SA)--

Working as an OpenERP/Odoo developer and a Team Leader 

Top 5 Odoo contributor On Stackoverflow




Sudhir Arya (SA)
On 1/5/15, 12:18 PM

Francisco Castro I guess it won't work.

There is another way to give domain at runtime. You can override fields_view_get method and append your domain to specific field(s).

Try following code:

from lxml import etree

def fields_view_get(self, cr, uid, view_id=None, view_type='form', context=None, toolbar=False, submenu=False):
        res = super(your_class_name, self).fields_view_get(cr, uid, view_id=view_id, view_type=view_type, context=context, toolbar=toolbar, submenu=submenu)
        doc = etree.XML(res['arch'])
        journal_ids = self._get_journal(cr, uid, context=context)
        for node in doc.xpath("//field[@name='journal_id']"):
            domain = "[('id', 'in', " + str(journal_ids) + ")]"
            node.set('domain', domain)
        res['arch'] = etree.tostring(doc)
        return res 

Hope this will help you.

this show me this error ValueError: Invalid domain term ('id', 'in', 114)

Francisco Castro
on 1/5/15, 12:49 PM

ok the fault was mine, on the _get_journal I was returning one element not a list so the "in " doesnt work. thx Sudhir Arya

Francisco Castro
on 1/5/15, 12:56 PM


| 5 3 6
Jakarta, Indonesia
On 1/6/15, 4:18 AM

Another way without having to mess up with fields_view_get is to set _get_journal to feed a function field, include that field in the view, and use that field in the domain.  If it is one2many or many2many fields, you can use field_name[0][2] to obtain the list of ids.

About This Community

This platform is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.


Odoo Training Center

Access to our E-learning platform and experience all Odoo Apps through learning videos, exercises and Quizz.

Test it now

Question tools

1 follower(s)


Asked: 1/5/15, 11:44 AM
Seen: 1168 times
Last updated: 3/16/15, 8:10 AM