3
votes

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'.

4
Can I know on which basis get_names will create name ?Ruchir Shukla
It will search another table at runtime & build the list & return it.Jibin
you can get the name in a field and assign it to the domain in the view.Shelton
What if its a list like [('name','in',get_names)] ? Will a char field work ?Jibin

4 Answers

2
votes

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?
2
votes

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
0
votes

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.

-3
votes

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.