1
votes

I created a new model 'sale.order.category' in order to group Sale Order Lines in specific subcategories (allowing to display subtotals, etc.)

class SaleOrderCategory(models.Model):
    _name = 'sale.order.category'
    name = fields.Char('Name', required=True)
    line_ids = fields.One2many('sale.order.line', 'category_id', 'Order Lines in this category')
    order_id = fields.Many2one('sale.order', 'Order', required=True, readonly=True)

class SaleOrder(models.Model):
    _name = 'sale.order'
    _inherit = 'sale.order'
    order_category_ids = fields.One2many('sale.order.category', 'order_id', 'Categories in this order', readonly=True, copy=True)

Just for info, here is my Order lines tree view modification to add the Category column :

<!-- adds a category column in the order lines list -->
<xpath expr="//field[@name='order_line']/tree/field[@name='name']" position="after">
    <field name="category_id"/>
</xpath>

My question is : how can I automatically populate the order_id field with the current Sales Order ID when I create a new Category through the Order Lines Tree (inside a Sales Order) ?

Many thanks, Max

1

1 Answers

0
votes

Preliminary remark: your use case seems related to what the official sale_layout module does, so you might want to have a look at it before going any further. Perhaps you could extend it instead of starting from scratch.

Next, the most basic answer to your question is to pass a default value for the order_id field of your sale.order.category model when you create it from the view. You can do that by setting a context with an appropriate default value on the many2one field from which you will create the value:

<xpath expr="//field[@name='order_line']/tree/field[@name='name']" position="after">
    <field name="category_id" context="{'default_order_id': parent.id}"/>
</xpath>

Your category_id field is defined on the sale.order.line tree view, so parent will dynamically refer to the parent record inside the web client interface, here the sale.order.

However this option will not work well:

  • When you're creating a new sales order, you will have to create your categories before the sales order is even saved, so there is no possible value for order_id yet. For this reason, you cannot make order_id required, and you will have to set its value again later, or you will need to save your orders before starting to add the categories.
  • You already have an order_lines one2many field in your sale.order.category model. The order_id field is redundant with the line_ids field, because all lines presumably belong to the same order.

A simple alternative would be to entirely omit the order_id field (use lines_id[0].order_id when you need it), or to replace it with a related field that will be automatically computed from the lines (it will take the value from the first order line):

order_id = fields.Many2one('sale.order', related='line_ids.order_id', readonly=True)

What you should do depends on your requirements, it's difficult to say based only on your question.