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.

2

How can I call a class function from an xml template

By
Krishna
on 7/2/16, 6:21 AM 382 views

This is my class and I want to get the "product.standard_price" in that xml template


class sale_order(models.Model):

    _inherit = 'sale.order'

    def _website_product_id_change(self, order_id, product_id, qty=0):

        context = dict(self.context or {})

        order = self.env['sale.order'].browse(order_id)

        product_context = self.context.copy()

        product_context.setdefault('lang', order.partner_id.lang)

        product_context.update({

        'partner': order.partner_id.id,

        'quantity': qty,

        'date': order.date_order,

        'pricelist': order.pricelist_id.id,

        })

        product = self.env['product.product'].browse(product_id, context=product_context)

        values = {

        'product_id': product_id,

        'name': product.display_name,

        'product_uom_qty': qty,

        'order_id': order_id,

        'product_uom': product.uom_id.id,

        'price_unit': product.standard_price,

        }

        if product.description_sale:

            values['name'] += '\n' + product.description_sale

            return values


This is my template

<template id="products_item_standard" inherit_id="website_sale.products_item" name="Products item standard">

    <xpath expr="//span[@itemprop='price']" position="replace">

        <span itemprop="price" style="display:none;" t-esc="product.standard_price"/>

        <span itemprop="priceCurrency" style="display:none;" t-esc="website.currency_id.name"/>

    </xpath>

</template>

2
Sebin Siby
On 7/4/16, 5:05 AM

  If it is in the same model then you can use,

<span t-esc="o.call_function1(o.amount_total)" />

Try it, and it may be work for you.

You can also refer this link,

https://www.odoo.com/forum/help-1/question/how-to-call-python-function-from-qweb-75122

Thank you Sebin but this code is not working yet get an error while am changing to your suggestion.

class res_partner(models.Model):

_inherit = "res.partner"

premium_user = fields.Boolean(string = "Premium User")

class sale_order(models.Model):

_name = 'cost.sale.order'

_inherit = 'sale.order'

def _website_product_id_changes(self, order_id, product_id, qty=0):

context = dict(self.context or {})

order = self.env['sale.order'].browse(order_id)

product_context = self.context.copy()

product_context.setdefault('lang', order.partner_id.lang)

product_context.update({

'partner': order.partner_id.id,

'quantity': qty,

'date': order.date_order,

'pricelist': order.pricelist_id.id,

})

product = self.env['product.product'].browse(product_id, context=product_context)

values = {

'product_id': product_id,

'name': product.display_name,

'product_uom_qty': qty,

'order_id': order_id,

'product_uom': product.uom_id.id,

'price_unit': product.standard_price,

}

if product.description_sale:

values['name'] += '\n' + product.description_sale

return values

def user_type(self):

if self.env.user.premium_user == True:

#a = _website_product_id_changes(self,product_id,context=context)

cPrice = self.env['cost.sale.order'].search([])

return cPrice,a

#return _website_product_id_changes(self,product_id,context=context)

else :

return self.env['sale.order'].browse(product.price)

This is my template

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

<openerp>

<data>

<record id="res_partner_inherit" model="ir.ui.view">

<field name="user.type.res.partner.inherit"/>

<field name="model">res.partner</field>

<field name="inherit_id" ref="base.view_partner_form"/>

<field name="arch" type="xml">

<data>

<xpath expr="//field[@name='parent_id']" position="after">

<label for="premium_user">Is Premium User</label>

<field name="premium_user"/>

</xpath>

</data>

</field>

</record>

<template id="user_type" inherit_id="website_sale.products_item" name="Products item standard">

<xpath expr="//span[@itemprop='price']" position="replace">

<t t-foreach= "cPrice" t-as="cost_cPriceS">

<span itemprop="price" style="display:none;" t-esc="cPrice.price_unit"/>

<span itemprop="priceCurrency" style="display:none;" t-esc="website.currency_id.name"/>

</t>

</xpath>

</template>

</data>

</openerp>

But I get the following error

foreach enumerator 'cPrice' is not defined while rendering template 'website_sale.products_item'

Krishna
on 7/5/16, 2:00 AM
0
Krishna
On 7/5/16, 12:18 AM

Thank you Sebin but this code is not working yet get an error while am changing to your suggestion.

class res_partner(models.Model):

    _inherit = "res.partner"

    premium_user = fields.Boolean(string = "Premium User")

class sale_order(models.Model):

    _name = 'cost.sale.order'

    _inherit = 'sale.order'

    def _website_product_id_changes(self, order_id, product_id, qty=0):

        context = dict(self.context or {})

        order = self.env['sale.order'].browse(order_id)

        product_context = self.context.copy()

        product_context.setdefault('lang', order.partner_id.lang)

        product_context.update({

            'partner': order.partner_id.id,

            'quantity': qty,

            'date': order.date_order,

            'pricelist': order.pricelist_id.id,

        })

        product = self.env['product.product'].browse(product_id, context=product_context)

        values = {

            'product_id': product_id,

            'name': product.display_name,

            'product_uom_qty': qty,

            'order_id': order_id,

            'product_uom': product.uom_id.id,

            'price_unit': product.standard_price,

        }

        if product.description_sale:

            values['name'] += '\n' + product.description_sale

            return values 

    def user_type(self):

        if self.env.user.premium_user == True:

        #a = _website_product_id_changes(self,product_id,context=context)

        cPrice = self.env['cost.sale.order'].search([]) 

        return cPrice,a

#return _website_product_id_changes(self,product_id,context=context)

        else :

            return self.env['sale.order'].browse(product.price)

This is my template

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

<openerp>

<data>

    <record id="res_partner_inherit" model="ir.ui.view">

        <field name="user.type.res.partner.inherit"/>

        <field name="model">res.partner</field>

        <field name="inherit_id" ref="base.view_partner_form"/>

        <field name="arch" type="xml">

            <data>

                <xpath expr="//field[@name='parent_id']" position="after">

                    <label for="premium_user">Is Premium User</label>

                    <field name="premium_user"/>

                </xpath>

            </data>

        </field>

    </record>

    <template id="user_type" inherit_id="website_sale.products_item" name="Products item standard">

        <xpath expr="//span[@itemprop='price']" position="replace">

            <t t-foreach= "cPrice" t-as="cost_cPriceS">

                <span itemprop="price" style="display:none;" t-esc="cPrice.price_unit"/>

                <span itemprop="priceCurrency" style="display:none;" t-esc="website.currency_id.name"/>

            </t>

        </xpath>

    </template>

</data>

</openerp>


But I get the following error

foreach enumerator 'cPrice' is not defined while rendering template 'website_sale.products_item'

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/2/16, 6:21 AM
Seen: 382 times
Last updated: 7/7/16, 1:39 AM