This question has been flagged
5 Replies
6158 Views

Hello Community,


in the one2many invoice_line_ids i want to make the field account_analytic_id required on some condition (Boolean field) is that possible using python or XML ?

Avatar
Discard
Best Answer

Hi Moubou,


If you are looking for the solution which make the field required in tree view based on the field value of main form than here is the solution for your problem.


Step 1:

Add one Boolean field on one2many model (This will be backend field you can make this field invisible once you achieve which you are looking for.

For e.g. consider you have taken boolean field with name "has_info"


Step 2: pass the value of the main form field based on which field you want to set required to your field in tree view.


for eg.

<field name="your_field_ids" context="{'condition_field':condition_field}"/>

Consider the condition_field is field defined in main model based on which you want to set required.


Step 3 :

Override default_get method of your one2many field's model.

In the default_get method you can set the boolean field value True or False according to the information coming from the main model field. which has been pass from context. the value of new boolean field can be set from the context of default get method.

If you don't know how to override default_get method, than i will provide you example later for this.


Step 4:

Apply the attrs on the field of one2many. like Amal shows in his example.


<field name="field_which_is_required_base_on_con"
  attrs="{'required':[('has_info','=',True)]} />
<!-- Consider that has_info is an example in my case, in your case whatever boolean field you take provide
your field name, condition is depends on you. -->

Do you let me know if you are failed to execute these steps.

Happy Odooing...

Hope this will help.

Regards,

Anil.


Avatar
Discard
Best Answer

You can add attribute to your XML code:

<field name="your_field"
  attrs="{'required':[('boolean_field','!=',True)]}" />

 

Avatar
Discard

Hi Mohammed Amal,

Are you sure about this answer???

Yes i've used something like this before

Okay...i have same issue few months before.but in my case, the boolean field is in account.invoice.

You can try passing value through context

for example

<field name="invoice_line_ids" context="{'default_boolean_field' : boolean_field}"/>