Search code examples
odooodoo-9odoo-12

Odoo, how to override an action extending it's domain (not overriding)


In a module that I do NOT own is the following act_window.

<record id="act_465" model="ir.actions.act_window">
    <field name="name">Act 465</field>
    <field name="res_model">stock.move</field>
    <field name="view_type">form</field>
    <field name="view_mode">tree,form</field>
    <field name="domain">[('available','=',True)]</field>
</record>

I would like to add the following AND criteria to the domain in my separate module:

('canceled', '!=', True)

I know I could OVERRIDE the act_window and the value of domain by [('available','=',True),('canceled', '!=', True)]. However doing so would totally remove what was already in place ('available','=',True). Meaning that if the owner of the base module change it's domain, I'll override his changes anyway.

Question

How could I EXTEND the domain by saying "I would like to add ('available','=',True) to the existing domain" ?


Solution

  • You can't, the action domain is a char field and there is no attribute to specify how to extend a specific record value.

    It is possible to use the function tag in the XML data file to call the write method and update the domain or create a function to update the action domain with a given domain list as a parameter.

    • Use the function tag to update the domain as a string (we suppose that the domain is set, list of tuples provided as a string)

      Example:

      <function model="ir.actions.act_window" name="write">
          <value eval="[ref('sale.action_quotations_with_onboarding')]"/>
          <value model="ir.actions.act_window" eval="{'domain':  obj().env['ir.actions.act_window'].browse(ref('sale.action_quotations_with_onboarding')).domain[:-1] + ', (\'invoice_status\', \'=\', \'to invoice\')]'}"/>
      </function>  
      
    • Create a function to update the domain as a list with a given domain as a parameter

      Example:

      class IrActionsActWindow(models.Model):
          _inherit = 'ir.actions.act_window'
      
          def update_domain(self, new_domain=None):
              if new_domain:
                  self.write({'domain': safe_eval(self.domain) + new_domain})
      

      Use the function to call the update_domain method:

      <function model="ir.actions.act_window"
                name="update_domain"
                eval="[ref('sale.action_quotations_with_onboarding'), [('invoice_status', '=', 'to invoice')]]"/>