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 to add the currency symbol after a monetary field with options ?

By
PY
on 12/10/13, 1:01 PM 5,486 views

Hi !

I try to add the currency symbol after a float field, but I'm stuck. Here's my code :

The python file:

def _get_your_company_currency(self, cr, uid, ids, field, args, context=None):
        res = {}
        for sr in self.browse(cr, uid, ids, context=None):
            res[sr.id] = self.pool.get('res.company').browse(cr, uid, sr.partner_id).currency_id
        return res

_columns = {
    'partner_id': fields.many2one('res.partner', 'Company', required=True),
    'cost': fields.float('Shipping cost', required=True),
    'currency_id': fields.function(_get_your_company_currency, type='char', method=True, string='Currency', readonly=True)
}

The xml:

<field name="cost" class="oe_inline" widget="monetary" options="{'currency_field': 'currency_id'}" />

Thanks in advance !

Edit : python file edited, issue still not solved

Edit 2 : I've also add this line to my xml file :

<field name="currency_id" invisible="1" />

Edit title

Better explanation Ok, I will explain my problem better... The company is a partner with the checkbox "Is company ?" checked. It's not "Your company", it's a supplier. I just need to put the currency symbol of my supplier into the form.

Nobody have any idea about how to solve this ?

PY
on 12/11/13, 9:13 AM
1

Felipe

--Felipe--
962
| 5 3 8
Bogotá, Colombia
--Felipe--
Informatics engineer Industrial engineer
Felipe
On 12/11/13, 10:52 AM

Your code have multiple issues:

You should know that the object res.partner is different from res.company(different names different objects), you are trying to browse a company with a partner object, you should browse a company with a company id.

Your function is returning a currency object, not the currency id(to access the id just put '.id' at the end of the object), your field is of type 'char' it should be a many2one to the object res.currency

You don't need to browse, you can just access the currency id of your company with sr.currency_id.id

you don't need a function for this, a related field is better than a function for this case.

I would recommend you to check the technical documentation. and also the functional documentation to document yourself better about the difference between a partner and company. https://doc.openerp.com/

I would implement it this way(for company):

In the .py the fields:

_columns = {
    'amount' : fields.float("amount", digits_compute=dp.get_precision('Account'))
    'company_id': fields.many2one('res.company', 'Company', required=True),
    'company_currency_id': fields.related('company_id','currency_id',type="many2one",
         relation="res.currency",string="Company Currency",readonly=True,store=True),
}

In the .xml

<field name="company_currency_id" invisible="1"/>
<field name="amount" widget="monetary" options="{'currency_field': 'company_currency_id'}"/>

First, thanks for your answer. The thing with partner is that I've already tried with company, but it didn't worked. Instead, I just added a domain in my xml like this : "[('is_company', '=', True)]". But I will try your code, for sure.

PY
on 12/11/13, 11:02 AM

Ok, I will explain my problem better... The company is a partner with the checkbox "Is company ?" checked. It's not "Your company", it's a supplier. I just need to put the currency symbol of my supplier into the form.

PY
on 12/11/13, 11:24 AM

afaik partner does not have a relation with res.currency, you must create one, then in the code I wrote change the 'res.company' for 'res.partner'

Felipe
on 12/11/13, 11:59 AM

So, I would glady create a new relation between res.partner and res.currency, but it seems wrong to me. Is there another way to know the currency of a partner ? As we know their nationality... well, I think the "purchase" module must have something...

PY
on 12/11/13, 12:57 PM

nations can have multiple official currencies. But I guess is not a bad idea to add a main currency field in the country object, but where to put that currency is something you can decide, that's the magic of open source.

Felipe
on 12/11/13, 2:44 PM

ok, thanks for your help anyway :)

PY
on 12/12/13, 9:08 AM

Finally, i've just used the currency of "Your company" (id = 1) : your_company = self.pool.get('res.company').browse(cr, uid, 1, context=context); your_currency_id = your_company.currency_id.id

PY
on 12/13/13, 10:09 AM
0
PY
On 12/11/13, 12:00 PM

Here's my new code, it works, but there's an error.

First, my python code:

class shipping_rule(osv.osv):
    _name = 'shipping_rule'
    _order = 'partner_id'

    def _get_currency(self, cr, uid, ids, field, args, context=None):
        res = {}
        for sr in self.browse(cr, uid, ids, context=context):
            res[sr.id] = sr.partner_id.company_id.currency_id.id
        return res

    _columns = {
        'name': fields.char('Rule name', size=128, required=True),
        'cost': fields.float('Shipping cost', required=True, digits_compute=dp.get_precision('Account')),
        'partner_id': fields.many2one('res.partner', 'Company', required=True),
        'company_currency_id': fields.function(_get_currency, type='many2one', method=True, string='Currency', readonly=True)
    }

XML

<sheet>
    <group string="Shipping rule">
        <group>
            <h1>
                <field name="name" placeholder="Name" />
            </h1>
        </group>
        <group>
            <field name="partner_id" domain="[('is_company', '=', True)]" />
            <field name="company_currency_id" invisible="1" />
        </group>
    </group>
    <group>
        <group>
            <div colspan="2">
                <label for="cost" class="oe_edit_only"/>
                <h3>
                    <field name="cost" class="oe_inline" widget="monetary" options="{'currency_field': 'company_currency_id'}" />
                </h3>
            </div>
        </group>
    </group>
</sheet>

error

Object Error

Object None doesn't exist

It's not happening when I delete the field "currency_id" in my xml, but then, the currency symbol doesn't appear next to my cost field.

How to fix this ?

SOLVED I've just modified my fields.function declaration like this :

'company_currency_id': fields.function(_get_currency, type='many2one', method=True, string='Currency', readonly=True, obj="res.currency")

you really need to check the documentation, the function field is not defined correctly, and the currency you are trying to capture is the currency from the company which that partner belongs, aka your company's and that field is only used in multi-company environments, you probably have that field as blank for all your partners(even if it is in a multi company environment).

Felipe
on 12/11/13, 12:16 PM

I have demo data, and no, the field is not blank. So, forget the partner_id field. If I wan't to add the currency related to my company, what must I do ? I don't know how to do that, either by id or by name.

PY
on 12/11/13, 12:25 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

0 follower(s)

Stats

Asked: 12/10/13, 1:01 PM
Seen: 5486 times
Last updated: 3/16/15, 8:10 AM