1
votes

I am returning a dynamic domain from a server action in Odoo with the following code. Please note I am using a hosted version of Odoo online, so I am doing this through the Odoo UI. Actions are returned by assigning action = {...}:

action = {
    'domain': {
        'route_id': [('id', 'in', record.x_all_route_ids.ids)]
    }
}

This server action is triggered when the product_id field changes for a sales.order.line. When viewing a sales order, if I change the product_id for one of the sales order lines, the domain for the route_id field is updated as expected.

However, the domain is changed for all of the route_id fields in the sale order line tree view. In other words, if there are 3 lines in the order, and I change the product_id for one of them, I want the domain to change for just that line's route_id field, not every route_id field in the sale order.

Is what I'm seeing the expected behavior, and is there any way to accomplish what I want instead?

Here is the compute method for the x_all_route_ids field:

for record in self:
    routes = record.product_id.x_all_route_ids

    if not record.product_id.x_is_preorder_active:
       # If the product has no stock, only keep routes that do not require stock
        if record.product_id.x_sellable_qty <= 0:
            routes = routes.filtered(lambda r: r.x_needs_available_inventory == False)

        # If a vendor is unavailable, remove it's route from the list
        for info in record.product_id.seller_ids:
            if not info.x_is_active:
                route = self.env['stock.location.route'].search([('x_is_vendor_dropship_route','=', True), ('x_vendor_id', '=', info.name.id)])
                routes = routes - route

    record['x_available_routes'] = [(6, False, routes.ids)]

And here is the code for the extension view:

<data>
    <xpath expr="//field[@name='order_line']/tree/field[@name='route_id']" position="before">
        <field name="x_available_routes" invisible="1"/>
    </xpath>
    <xpath expr="//field[@name='order_line']/tree/field[@name='route_id']" position="attributes">
        <attribute name="domain">[('id', 'in', x_available_routes[0][2])]</attribute>
    </xpath>
1

1 Answers

0
votes

In Odoo you can directly assign values.

record.x_available_routes=routes.ids

for record in self:
    routes = record.product_id.x_all_route_ids
    if not record.product_id.x_is_preorder_active:
       # If the product has no stock, only keep routes that do not require stock
        if record.product_id.x_sellable_qty <= 0:
            routes = routes.filtered(lambda r: r.x_needs_available_inventory == False)

        # If a vendor is unavailable, remove it's route from the list
        for info in record.product_id.seller_ids:
            if not info.x_is_active:
                route = self.env['stock.location.route'].search([('x_is_vendor_dropship_route','=', True), ('x_vendor_id', '=', info.name.id)])
                routes = routes - route

    record.x_available_routes=routes.ids

In xml you should use following code.

<xpath expr="//field[@name='order_line']/tree/field[@name='route_id']" position="attributes">
    <attribute name="domain">[('route_id', 'in', x_available_routes[0][2])]</attribute>
</xpath>