1
votes

I'm trying to add a domain to an Odoo field for a sale.order.line. Specifically, I'm trying to ensure that the only Route available to choose are ones with the same name as the product related to the order line. Here is my attempt:

Here is the original out-of-the-box field definition for route_id on a sale.order.line:

 <field name="route_id" groups="sale_stock.group_route_so_lines">

I modified it to include a domain like so:

<field name="route_id" groups="sale_stock.group_route_so_lines" domain="[('name', '=', product_id.name)]"/>

However, I seem to be unable to access the related product.product via the product_id attribute. From what I can tell, the product_id is simply returning the id, rather than the actual record itself. Therefore, when I try to chain the call to name, it doesn't work.

I get the following error when clicking into the Routes field on the order line:

Uncaught Error: AttributeError: object has no attribute 'name'

Does anybody know the proper way to add this domain to the view?

1

1 Answers

0
votes

You are right in that product_id is an ID rather than a record, so chain calling will not work during domain evaluation. In order for the domain to be evaluated correctly, the field that's value is used in the domain must exist in the view.

To achieve this, you can add a related field to sale.order.line model, pointing to product_id.name:

from openerp import fields, models


class SaleOrderLine(models.Model):
    _inherit = 'sale.order.line'

    product_name = fields.Char(
        related='product_id.name',
        string='Product Name',
        store=True,
    )

Then, in your view's XML definition, you would need to add the product_name field to the view, in order to use it inside the domain. You can set the invisible attribute to 1 in order not to display it in the form. Now the field can be used in the domain:

<field name="product_name" invisible="1"/>
<field name="route_id" groups="sale_stock.group_route_so_lines" 
       domain="[('name', '=', product_name)]"/>