This question has been flagged
2 Replies
5398 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))
    else:
        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

 

Avatar
Discard
Best Answer

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.

Avatar
Discard
Author

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

Author

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

Best Answer

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.

Avatar
Discard