This question has been flagged

Hello everyone,

I am using odoo 12 and need to filter information shown on a many2one field, based on a condition that depends on 2 fields of type selection. ​

​Model 1:

    class MymodulePayment(models.Model):
        _name = 'mymodule.payment'
        _description = 'My Module Payment'

        code = fields.Char('Payment Code', size=2, required=True)
        name = fields.Char('Payment Name', required=True)
        active = fields.Boolean('Is Active', default=True)
        #This is the selection field from model 1
       eligibility = fields.Selection(string="Apply to", selection=[('cash', 'Cash Journals'),('bank', 'Bank Journals'), ('both', 'Both Cash/Bank')], required=True, default="both", )

Model 2: (account.journal)

    class AccountJournal(models.Model):
         _inherit = "account.journal"

         #Field "type" is core Odoo field
         type = fields.Selection([('sale', 'Sale'),('purchase', 'Purchase'),('cash', 'Cash'),('bank', 'Bank'),('general', 'Miscellaneous'),], required=True,)
         payment_mode_id = fields.Many2one('mymodule.payment', 'Default Payment Mode',)
         #This is the related selection field on model 2
         eligibility = fields.Selection(related='payment_mode_id.eligibility')​

Using this approach, I can access model1 (by using related many2one field) and also have access to the field "eligibility" which relates the fields from 2 models.

Now, I need to compare both fields "type" and "relation_eligibility" from model2 and depending on result, I want to filter the information to be shown on "relation_mode11_id".
That is, on relation_mode11_id, I want to show all records which "eligibility" is equal to value of "type" field, or the value of "eligibility" is "both"

Since I have 2 conditions and only need one to be true, I am using '|' ("or" operator).

The XML for the new fields on account.journal:

     <record id="mymodule_payment_journal_form_view" model="ir.ui.view">
          <field name="name">My Module Payment Mode Journal View</field>
          <field name="model">account.journal</field>
          <field name="inherit_id" ref="account.view_account_journal_form"/>
          <field name="arch" type="xml">
               <xpath expr="//field[@name='outbound_payment_method_ids']" position="after">
                    <field name="eligibility" invisible="1"/>
                    <field name="type" invisible="1"/>
                    <field name="payment_mode_id" domain="['|',('eligibility','=','type'),('eligibility','=','both')]" attrs="{'required': [('type','in',['cash','bank'])],'invisible': [('type','not in',['cash','bank'])]}"/>
               </xpath>
          </field>
     </record>

I am always getting the same error:

ValueError: Invalid field 'eligibility' in leaf "<osv.ExtendedLeaf: ('eligibility', '=', 'type') on model1 (ctx: )>"​

If I remove the domain, I can see all values on  payment_mode_i but need to show only the records with the above conditions on the selection list.

I have noticed that the error is not on the domain itself because tried do manually set values on the domain (ie, 'cash', 'bank' and using only one condition and the error still the same.

How can I achieve this?

Thank you in advance

Best regards

PM

Avatar
Discard
Best Answer

you have to declare field in view like

​<field name="saft_eligibility" invisible="1"/>
<field name="type" invisible="1"/>
<field name="relation_eligibility" invisible="1"/>
<field name="relation_mode11_id" domain="['|',('relation_eligibility','=','type'),('relation_eligibility','=','both')]"/>

Avatar
Discard
Author

Dear @Ravi,

Tried to use your approach and did not worked.

I have noticed that perhaps the problem is not on the domain.

I have tried to use only one domain and manually set values for the condition and still does not work.

I have updated the answer with the full code for both models and XML view. Can you have a look?

Thank you very much

I think the issue is with [('eligibility','=','type')] where RHS leaf `type` is not define/available in selection tuple

eligibility = fields.Selection(string="Apply to", selection=[('cash', 'Cash Journals'), ('bank', 'Bank Journals'), ('both', 'Both Cash/Bank')], required=True, default="both", ) Here `type` key is not define

Author

Thank you Ravi.

I have tried to use: domain="[('relation_eligibility','=',type)]" (removed '') and I can see that the error now shows the journal type I am working with (bank or cash), even thinking that I am still getting the same error.

Also tried to manually set the desired domain value, for instance "cash" using: domain="[('relation_eligibility','=','cash')]" and I am getting the same error:

"ValueError: Invalid field 'eligibility' in leaf "<osv.ExtendedLeaf...."

Nota here that "cash" key is defined on both journal type and my module eligibility field.

Any idea?

Author

Perhaps the error not on the domain itself....

Perhaps something with field declaration or perhaps Odoo do not allow to work with selection fields this way???

ref: https://github.com/odoo/odoo/blob/12.0/odoo/osv/expression.py#L829

error raised because field not defined in the model

field = model._fields.get(path[0])

if not field:

print('>>>>>>>>>>.', model, path[0])

path[0] field did not define in model

Author

Thank you once again @Ravi Gadhia

You solved my problem.

For some reason I had to use the exact same name for the field "eligibility" on both models.

Also, the final domain I have used:

domain="['|',('eligibility','=',type),('eligibility','=','both')]"

It's working exactly as expected.

+1000 for you.

Thanks once again