Odoo Help

Welcome!

This community 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.

0

using function in domain filter

By
Adrian
on 12/7/13, 6:12 AM 10,741 views

i want to use a function's return as a domain filter.

'product_id' : fields.many2one('product.product', 'products', domain=[('id', 'in',lambda self,cr,uid,ids, context: self._get_technic_warehause(cr,uid,ids,context))])

like that but it sadly wont work.... this is the error i get

2013-12-07 09:59:49,686 16226 ERROR test werkzeug: Error on request:
Traceback (most recent call last):
  File "/usr/lib/python2.7/dist-packages/werkzeug/serving.py", line 159, in run_wsgi
    execute(app)
  File "/usr/lib/python2.7/dist-packages/werkzeug/serving.py", line 146, in execute
    application_iter = app(environ, start_response)
  File "/opt/openerp/server/openerp/service/wsgi_server.py", line 417, in application
    return application_unproxied(environ, start_response)
  File "/opt/openerp/server/openerp/service/wsgi_server.py", line 403, in application_unproxied
    result = handler(environ, start_response)
  File "/opt/openerp/web/addons/web/http.py", line 528, in __call__
    return self.dispatch(environ, start_response)
  File "/opt/openerp/web/addons/web/http.py", line 487, in __call__
    return self.app(environ, start_wrapped)
  File "/usr/lib/python2.7/dist-packages/werkzeug/wsgi.py", line 411, in __call__
    return self.app(environ, start_response)
  File "/opt/openerp/web/addons/web/http.py", line 553, in dispatch
    result = handler(request)
  File "/opt/openerp/web/addons/web/http.py", line 618, in <lambda>
    return lambda request: JsonRequest(request).dispatch(method)
  File "/opt/openerp/web/addons/web/http.py", line 251, in dispatch
    body = simplejson.dumps(response)
  File "/usr/share/pyshared/simplejson/__init__.py", line 321, in dumps
    return _default_encoder.encode(obj)
  File "/usr/share/pyshared/simplejson/encoder.py", line 237, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "/usr/share/pyshared/simplejson/encoder.py", line 311, in iterencode
    return _iterencode(o, 0)
  File "/usr/share/pyshared/simplejson/encoder.py", line 213, in default
    raise TypeError(repr(o) + " is not JSON serializable")
TypeError: <function <lambda> at 0x535c230> is not JSON serializable

the function returns a list of Ids, but here is the function itself:

def _get_technic_warehause(self, cr, uid,ids, context=None):
        data = self.browse(cr, uid, ids, context=context)[0]
        pdb.set_trace()
        if data.technic_id.id:
            location_obj = self.pool.get('stock.location')
            lines_ids = location_obj.search(cr, uid, [('user_id', '=', data.technic_id.id)])
            #res = {data['id'] : []}
            res = []
            for line_id in lines_ids:
                prod_info = location_obj._product_get(cr, uid, line_id, context=context)
                for prod_id in prod_info.keys():
                    if prod_info[prod_id] != 0.0:
                        #res[data['id']].append(prod_id)
                        res.append(prod_id)
            return res
        #return {}

is there anyway in openerp to make this work? I know that openerp doesn't allow function fields in domain filter sadly, but anyway to get around it?

2

Hello there,

I found out how to (very simple) solve this problem. I'm working with Odoo v8 (I don't know if it's working with v7), and all you have to do is to define a function that will return a JSON serializable domain (in fact, a domain as if you had put it directly in your field definition). Here's me code :

class myClass(models.Model)

    ...

    @api.model
    def _getUserGroupId(self):
        return [('groups_id', '=', self.env.ref('module.xml_id').id)]

    rel_ids = fields.Many2many(comodel_name='res.users', relation='my_relation_table_name', column1='rel_id', column2='user_id', string='Relation field', domain=_getUserGroupId)

Hope this helps.

 

1
klacus
On 5/15/14, 12:34 AM

Hi.

You can avoid the problem with the new field in python.  This field is must point a fiunction (field.function) and call the function with this. Call the procedure with this function, and the field will be get the result of the function. The doamin will be accept it, you need to hide the new field and you finish with this.

 

 

Sorry for the original hungarian post. :-) +2 pls. :-)

L.

0
Abdul Nazar
On 10/26/16, 9:44 AM

Here i am going to give a example of filter employee by his related user.

In Python:

@api.model    def _get_emp_domain(self):
    domain = []    ctx = self.env.context
    if ('user' in self.env.context and 'user' in self.env.context and 'cond' in self.env.context) and ctx.get('val') and ctx.get('cond') and ctx.get('val'):
        domain = [(ctx.get('user'),ctx.get('cond'),ctx.get('val'))]
    return domain


employee_id = fields.Many2one('hr.employee', string="Employee", domain=lambda self:self._get_emp_domain(), required=True,  default=_get_employee)


In XML:

<record id="action_my_custom" model="ir.actions.act_window">

        <field name="name">My List</field>

        <field name="res_model">my.custom</field>

        <field name="view_type">form</field>

        <field name="view_mode">tree,form</field>

        <field name="domain">[('employee_id.user_id','=',uid)]</field>

        <field name="context">{'user': 'user_id','cond': '=','val': uid}</field>

        <field name="view_id" eval="False"/>

        <field name="search_view_id" ref="view_my_custom_filter"/>

    </record>


I hope it will be helpful for you....

 
          

0
bernscheinder
On 6/13/14, 7:22 AM

Hi adrian,

Have you solved this bug please, I need something like this!

 

Sincerly,

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

3 follower(s)

Stats

Asked: 12/7/13, 6:12 AM
Seen: 10741 times
Last updated: 10/26/16, 9:44 AM