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