3

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?

Don Kirkby
  • 53,582
  • 27
  • 205
  • 286
Santu Srinu
  • 101
  • 3
  • 11
  • You question is quite hard to follow: it sounds like it could benefit from some code examples explaining the current state of your problem. – jro Nov 30 '11 at 14:53
  • I tried to clarify your question, @Santu. Did I understand it correctly? – Don Kirkby Nov 30 '11 at 23:20
  • I notice that you haven't accepted any answers to your other questions, @Santu. If any of the answers were helpful, please mark them as accepted by clicking on the big check mark next to each answer. That helps other people who have the same question, and it's a way to thank the people who helped you. – Don Kirkby Nov 30 '11 at 23:22
  • try on_change function i think it works – Pravitha V Apr 19 '12 at 05:37

3 Answers3

2

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.

Community
  • 1
  • 1
Don Kirkby
  • 53,582
  • 27
  • 205
  • 286
1

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

OmaL
  • 5,037
  • 3
  • 31
  • 48
1

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.

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