I am facing this problem:
"I need to change the domain of a field from within an on_change event handler because it needs special computation. While it works as expected when the field is changed, the event isn't triggered when the value is loaded into the field at form init time."
so i tried: 1. on_change: not called when editing form, and that is reasonable. 2. Just using a domain: i found my domain is more complex and needs special computation. 3. 'fields_view_get': i couldn't get the current values to construct my domain. 4. custom function to get domain: i am too close with this, but have a problem with return type.
So what is the best alternative for on_change, for this case?
here is a summarized code for '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='user_id']"):
node.set('domain', 'MY DOMAIN') # here i need the current form values (if applicable)
res['arch'] = etree.tostring(doc)
return res
Code for custom function:
def _get_user_id_domain(self, cr, uid, ids, field_name, arg, context=None):
record_id = ids[0]
project_id = self.browse(cr, uid, ids, context=context)[0].project_id # here i can get the values (eg. 'project_id')
# do some computation....
return {record_id: "MY DOMAIN"} # returning domain as string rises an exception*
and then i made a field to hold domain value:
'domain_field': fields.function(_get_user_id_domain, type='char', size=255, method=True, string="Domain"),
and i use it in XML:
<xpath expr="//field[@name='user_id']" position="replace">
<field name="user_id" domain="domain_field" />
</xpath>
*When edit the filed, this exception occur "TypeError: second argument to Function.prototype.apply must be an array", (only if i returned domain as string, and i need to do that!)
Am i going in the wrong direction, and what is the best way to make such a domin?
Sorry for posting all these details, it may solve another question :)
I found a way to avoid returning domain as string in last function, and it works.
Can you please share your solution with us?