Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

0

How do you refer in XML a new field added to sales.order.line in the same module?

By
E.M.
on 7/25/15, 11:12 AM 532 views

I am creating a module which defines a new field named "sale_margin_percent" to sales.order.lines, and then a xml file which adds the field to forms and tries to add an "on_change" attribute to the new generated field.

sales_order_percent.py (note that sale_margin_percent field is defined)

# -*- coding: utf-8 -*-
from openerp import models, fields, api
class sale_order_line(models.Model):

_inherit = "sale.order.line"

sale_margin_percent = fields.Float('Margen de Ventas (%)', (4, 2))

def update_sale_margin(self, cr, uid, ids, price_unit, purchase_price, discount):
print 'DUMMY'

    def update_sale_price(self, cr, uid, ids, sale_margin_percent, purchase_price, discount):
       print 'DUMMY'


XML:

First sale_margin_order is added, and then an "on_change" attribute is tried to be added without success

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

<record model="ir.ui.view" id="sale_margin_percent_2">
<field name="name">sale.margin.percent.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="sale_margin_percent" groups="base.group_user"/>
</xpath>
</field>
</record>

<record model="ir.ui.view" id="sale_margin_percent_3">
<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="sale_margin_percent" groups="base.group_user"/>
</xpath>
</field>
</record>


<record model="ir.ui.view" id="sale_margin_percent_u3">
<field name="name">sale.margin.percent.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='sale_margin_percent']" position="attributes">
<attribute name="on_change">update_sale_price(sale_margin_percent, purchase_price, discount)</attribute>
</xpath>
</field>
</record>

</data>
</openerp>


ERROR:

ParseError: "ValidateError
Field(s) `arch` failed against a constraint: Invalid view definition

Error details:
Element '<xpath expr="//field[@name='order_line']/tree//field[@name='sale_margin_percent']">' cannot be located in parent view

Error context:
View `sale.margin.percent.view.form`
[view_id: 733, xml_id: n/a, model: sale.order, parent_id: 577]" while parsing /usr/lib/python2.7/dist-packages/openerp/addons/custom/sale_margin_percent/sale_margin_percent.xml:38, near
<record model="ir.ui.view" id="sale_margin_percent_u3">
<field name="name">sale.margin.percent.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='sale_margin_percent']" position="attributes">
<attribute name="on_change">update_sale_price(sale_margin_percent, purchase_price, discount)</attribute>
</xpath>
</field>
</record>

1

zbik

--zbik--
5941
| 6 4 7
Lodz, Poland
--zbik--


zbik
On 7/25/15, 12:32 PM

1. Do you know the module sale_margin?

2. Try this:

<record model="ir.ui.view" id="sale_margin_percent_u3">
<field name="name">sale.margin.percent.view.form</field>
<field name="model">sale.order</field>
<field name="inherit_id" ref="sale_margin_percent_3"/>
<field name="arch" type="xml">
<xpath expr="//field[@name='order_line']/tree//field[@name='sale_margin_percent']" position="attributes">
<attribute name="on_change">update_sale_price(sale_margin_percent, purchase_price, discount)</attribute>
</xpath>
</field>
</record>



Thanks a lot, now I understand this much better and I even got two ways of doing it. Regarding first question: I do know module sale_margin; but sale_margin does not provide sale margin per line, just sale margin for the whole sale order. Additionally, it provides absolute margin, where most of sales chaps use sale margin in %, which is what they are used to all around the planet. Thanks a lot for your answers, they really help.

E.M.
on 7/25/15, 1:12 PM

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

1 follower(s)

Stats

Asked: 7/25/15, 11:12 AM
Seen: 532 times
Last updated: 7/25/15, 1:12 PM