3

I want to give a dynamic domain equation in view or in field definition in .py.

like

<field name="product_id" domain="[('name','in',get_names)]"/>

product_id is a many2one field.

get_names is function that creates a list at run time.

Its showing an error - "name 'get_names' is not defined"

Any Ideas.

I have also tried the following.

'product_id': fields.many2one('mymodule.relation.model','Title',selection=get_names)

This displays all entries in mymodule.relation.model. The only thing it does is to validate if value selected/submittted by user belongs to the 'get_names'.

Jibin
  • 3,054
  • 7
  • 36
  • 51

4 Answers4

2

inherit the fields_view_get() function and manage the domain condition. Please check these posts

  1. How to create a dynamic view on OpenERP
  2. How can I change the choices in an OpenERP selection field based on other field values?
Community
  • 1
  • 1
OmaL
  • 5,037
  • 3
  • 31
  • 48
2

1- You can use function field like this:

def _get_domain(self, cr, uid, ids, field_name, arg, context=None):
    record_id = ids[0] 
    # do some computations....
    return {record_id: YOUR DOMAIN} 

and function field:

'domain_field': fields.function(_get_domain, type='char', size=255, method=True, string="Domain"),

And use the name of the field in xml (domain attr):

<field name="product_id" domain="domain_field" />

2- you can use 'fields_view_get':

def fields_view_get(self, cr, uid, view_id=None, view_type='form', context=None, toolbar=False, submenu=False):
    res = super(taskmng_task, 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'])
    for node in doc.xpath("//field[@name='project_id']"):
        # do some computations....
        node.set('domain', YOUR DOMAIN)
    res['arch'] = etree.tostring(doc)
    return res
Luis Masuelli
  • 12,079
  • 10
  • 49
  • 87
0

You can't use a function or method in the domain expression, only object fields. It's not equivalent, but closest thing is to create a function field to use in the domain expression.

Daniel Reis
  • 12,944
  • 6
  • 43
  • 71
-3

As Don't know your exact requirement .but may be any one from these 2 can help you

http://ruchir-shukla.blogspot.in/2010/11/domains-value-depending-on-condition.html

or check the Account Invoice product onchange . You can return domain from the onchange.

Ruchir Shukla
  • 805
  • 6
  • 13
  • I checked your link.It is domain based on condition.My requirement is that the right argument in domain should be dynamic. – Jibin Nov 08 '12 at 11:00
  • dynamic but based on some field value? the write onchang of that field and return domainexample is there in invoice line product onchange sale order line product onchange – Ruchir Shukla Nov 08 '12 at 11:41
  • It would be nice to include your answer here instead of posting it in a blog. – Daniel Reis Nov 08 '12 at 21:47
  • It would be great if you check the blog date. It was 2 years back I created blog for same topic , here I AM JUST GIVING REF. OF SAME. – Ruchir Shukla Nov 10 '12 at 03:18