Search code examples
odoo-11

Odoo 11: Domain on many2many field not working


<record model="ir.actions.act_window" id="action_purchase_orders">
    <field name="name">Purchase Orders</field>
    <field name="type">ir.actions.act_window</field>
    <field name="res_model">purchase.order</field>
    <field name="view_type">form</field>
    <field name="view_mode">tree,form</field>
    <field name="domain">[('id', 'in', purchase_order_ids.ids)]</field> <------- This is not working.
</record>


<record id="purchase.form" model="ir.ui.view">
    <field name="name">purchase.form</field>
    <field name="model">purchase.form</field>
    <field name="arch" type="xml">
        <form string="Purchase Form">                
            <sheet>                    
                <div class="oe_button_box" name="button_box">
                    <button type="action" name="%(action_purchase_orders)d" class="oe_stat_button" icon="fa-list-alt"
                            attrs="{'invisible':[('purchase_order_count', '=', 0)]}">
                        <field name="purchase_order_count" invisible="1"/>
                        <field name="purchase_order_ids" string="Purchase Orders" widget="statinfo" help="Purchase orders"/>
                    </button>
...

class PurchaseForm(models.Model):
    _name = 'purchase.form'

    @api.depends('purchase_order_ids')
    def _purchase_order_count(self):
       for pp in self:
           pp.purchase_order_count = len(pp.purchase_order_ids)

    purchase_order_ids = fields.Many2many('purchase.order', 'purchase_form_purchase_order_rel', 'purchase_form_id', 'purchase_order_id', 'Purchase Orders', states=READONLY_STATES)
    purchase_order_count = fields.Integer(compute='_purchase_order_count', string='# of Purchase Orders')

Error:

Uncaught Error: NameError: name 'purchase_order_ids' is not defined
http://localhost:8069/web/content/801-4161526/web.assets_backend.js:144
Traceback:
Error: NameError: name 'purchase_order_ids' is not defined
    at PY_ensurepy (http://localhost:8069/web/content/801-4161526/web.assets_backend.js:144:65)
    at Object.py.evaluate (http://localhost:8069/web/content/801-4161526/web.assets_backend.js:259:8)
    at Object.py.evaluate (http://localhost:8069/web/content/801-4161526/web.assets_backend.js:267:99)
    at Object.py.evaluate (http://localhost:8069/web/content/801-4161526/web.assets_backend.js:268:194)
    at Object.py.eval (http://localhost:8069/web/content/801-4161526/web.assets_backend.js:272:284)
    at http://localhost:8069/web/content/801-4161526/web.assets_backend.js:377:110
    at iterator (http://localhost:8069/web/content/800-e6de2a4/web.assets_common.js:14:183)
    at Function.<anonymous> (http://localhost:8069/web/content/800-e6de2a4/web.assets_common.js:17:8)
    at _.(anonymous function) [as reduce] (http://localhost:8069/web/content/800-e6de2a4/web.assets_common.js:69:526)
    at eval_contexts (http://localhost:8069/web/content/801-4161526/web.assets_backend.js:375:136)

Solution

  • You cannot do that in the action_purchase_orders view action, you cannot use a field in the right side of domain tuples, because there will be no context to supply that value. To get it done you will need to change your button to be of type object and using a python method associated return the action dict with the desired domain values evaluated so the domain only contain values for their right side tuples. Like(see the new type and name for the button):

    <button type="object" name="action_purchase_orders" class="oe_stat_button" icon="fa-list-alt" attrs="{'invisible':[('purchase_order_count', '=', 0)]}">
        <field name="purchase_order_count" invisible="1"/>
        <field name="purchase_order_ids" string="Purchase Orders" widget="statinfo" help="Purchase orders"/>
    </button>
    

    In your purchase.form model define the method action_purchase_orders, like:

    @api.multi
    def action_purchase_orders(self):
        self.ensure_one()
        return {
            'name': _('Purchase Orders'),
            'type': 'ir.actions.act_window',
            'view_type': 'form',
            'view_mode': 'form',
            'res_model': 'purchase.order',
            'target': 'new',
            'domain': [('id', 'in', self.purchase_order_ids.ids)]
            'context': self.env.context.copy(),
        }