Skip to Content
Menu
This question has been flagged

I am trying to add a field to sale.order.form.

Specifically, I am trying to add x_sale_margin field to sale.order.lines so there is information on the sale margin for every sales order line. I have managed to complete the task by modifying everything in the GUI but now I am trying to get it working via module.

 I have tried this:

sale_margin_lines.py

# -*- coding: utf-8 -*-
from openerp import models, fields, api
class SaleMarginLines(models.Model):
_inherit = 'sale.order.line',
x_sale_margin = fields.Float('Margen de Ventas (%)', (4, 2))


sale_margin_lines.xml

<?xml version="1.0" encoding="utf-8"?>
<openerp>
<data>

<record model="ir.ui.view" id="sale_margin_lines_sale_order">
<field name="name">sale.order.margin.lines.view.form</field>
<field name="model">sale.order</field>
<field name="inherit_id" ref="sale.view_order_form"/>
<field name="arch" type="xml">
<xpath expr="//group[@name='sale_total']" position="after">
<group name="sale_margin_grp">
<field name="x_sale_margin" groups="base.group_user"/>
</group>
</xpath>
</field>
</record>

<record model="ir.ui.view" id="sale_margin_lines_sale_order_line">
<field name="name">sale.order.line.margin.lines.view.form</field>
<field name="model">sale.order</field>
<field name="inherit_id" ref="sale.view_order_form"/>
<field name="arch" type="xml">
<xpath expr="//field[@name='order_line']/form//field[@name='price_unit']" position="after">
<field name="x_sale_margin" groups="base.group_user"/>
</xpath>
</field>
</record>

<record model="ir.ui.view" id="sale_margin_lines_sale_order_line_form">
<field name="name">sale.order.line.tree.margin.view.form</field>
<field name="model">sale.order</field>
<field name="inherit_id" ref="sale.view_order_form"/>
<field name="arch" type="xml">
<xpath expr="//field[@name='order_line']/tree//field[@name='price_unit']" position="after">
<field name="x_sale_margin" groups="base.group_user"/>
</xpath>
</field>
</record>
</data>
</openerp>
But I got an error that states that "x_sale_margin" field is not defined. I wonder why If it is actually being defined by the module (and without the XML the field can be seen in configuration -> database -> models)

Thanks,

P.D.

I also include the __init__.py and __openerp__.py files in case it helps to reproduce or to other people learning how to write modules.


__init__.py

 from . import sale_margin_lines


__openerp__.py

 {

'name': 'Sales Margin in Sales Orders',

'description': 'Provides sales margin information in Sales Orders, both line by line and in total order',

'author': 'E.M.',

'depends': ['sale','sale_margin'],

'data': ['sale_margin_lines.xml'],

}

Avatar
Discard
Best Answer

Hello, In this code you try to put field in sale.order object because sale_total is out of sale.order.line view.

If you inherit sale.order.line than you can put field in sale.order.line view only. In sale order form view there is inner view of sale.order.line so put field in only sale.order.line inner view.

May be this will help you.

 <xpath expr="//group[@name='sale_total']" position="after">
    <group name="sale_margin_grp">
    <field name="x_sale_margin" groups="base.group_user"/>
</group>
</xpath>
 
Avatar
Discard
Author

Thanks, I used sale_margin module as template and it uses sale_total which effectively I understood is outside sale_order_line. Instead your solution I removed the firstof the three blocks I used and it works. That was what I was actually looking for.

Author

By block I mean "".

Related Posts Replies Views Activity
2
Aug 16
9553
9
Apr 22
27377
1
Jul 16
5188
0
Feb 16
3587
1
Jul 15
4729