How can I change the choices in an OpenERP selection field based on other field values?

时光总嘲笑我的痴心妄想 提交于 2019-11-30 17:53:07

问题


I have a form with four fields:

  • Crop - selection
  • Active From - date
  • Active To - date
  • Block Area - selection

How can I make the available options in Block Area depend on the values the user selects for the other fields?


回答1:


I don't know if you can do it with a selection field, but you can change the domain of a many-to-one field when another field changes value. You might also be able to just use the other fields in your BlockArea field's domain, and not have to change it at all. Look at the way the partner address screen sets the domain for the state_id field. You might find this related question helpful.

If you do need to change the domain when another field changes, then the on_change event can include a domain entry in the dictionary it returns.

I found a discussion thread that says you can use the selection widget on a many-to-one field, so that might work for you if you set a domain for the field. I haven't tried it myself.




回答2:


To limit the available options based on other field values you can use the domain. As an example, this is used on the standard module project_issue:

Quoting the relevant lines:

class project_issue(crm.crm_case, osv.osv):
    _columns = {
        'project_id':fields.many2one('project.project', 'Project'),
        'type_id': fields.many2one ('project.task.type', 'Stages', domain="[('project_ids', '=', project_id)]"),
    }

In this example the type_id available options are fetched from project.task.type table, depending on the value of the project_id field.




回答3:


Try on_change function.. create an on_change function and at the end of the function return the domain condition for the field block_area for example

def onchange_for_block_area(self,cr,uid,ids,crop,from_date,to_date,context):
    domain=[]
    #
    #some statements for finding the domain
    #
    return {'domain':{'block_area': domain}}

provide the onchange function on the fields crop, from_date and to_date



来源:https://stackoverflow.com/questions/8325315/how-can-i-change-the-choices-in-an-openerp-selection-field-based-on-other-field

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!