Siirry sisältöön
Menu
Sinun on rekisteröidyttävä, jotta voit olla vuorovaikutuksessa yhteisön kanssa.
Tämä kysymys on merkitty
2 Vastaukset
47770 Näkymät

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.

Avatar
Hylkää
Tekijä

Nobody have any idea about how to solve this ?

Paras vastaus

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'}"/>
Avatar
Hylkää
Tekijä

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.

Tekijä

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.

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'

Tekijä

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...

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.

Tekijä

ok, thanks for your help anyway :)

Tekijä

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

Tekijä Paras vastaus

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")
Avatar
Hylkää

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).

Tekijä

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.

Aiheeseen liittyviä artikkeleita Vastaukset Näkymät Toimenpide
1
huhtik. 24
1808
2
tammik. 24
1831
Error message Ratkaistu
2
marrask. 23
2740
1
maalisk. 24
2067
4
syysk. 23
16851