Skip to Content
Menu
This question has been flagged
3 Replies
8957 Views

Hi, I've made a custom module with two models, let's call them "a.py" and "b.py".
I'll write down the important part of the two models for context

A.PY
class A(models.Model):
_name = 'a'
codice = fields.Char(string="Codice del corso", required=True)
costo_a_persona = fields.Monetary(string="Costo (a persona)", required=True)
currency_id = fields.Many2one("res.currency", string="Valuta", required=True)
a_b = fields.One2many('b', 'b_a', string="Edizioni del corso erogate:")
B.PY
class B(models.Model):
_name = 'b'
number = fields.Text(string="Numero edizione", required=True) b_a = fields.Many2one('a', string="Edizione del corso:", required=True)

I would like to give to "b.py" the Monetary and currency_id field (I'll not explain why to simplify, I just need them there too), but if I try to do it in the following way then when I restart Odoo and upgrade the module it would give me back the error:

B.PY
class B(models.Model):
[...]currency_corso_id = fields.Many2one("res.currency", string="Valuta", required=True)
costo_corso_a_persona = fields.Monetary(related="b_a.costo_a_persona", string="Costo a persona", store=True)
-> AssertionError: Field b.costo_corso_a_persona with unknown currency_field None
Avatar
Discard
Author

@sonia thanks!! now it works :)

Best Answer

Try changing the currency field in "b.py" with a standard:

currency_id = fields.Many2one("res.currency", string="Valuta", required=True)

 And then specifying the currency field in the monetary field:

costo_corso_a_persona = fields.Monetary(related="b_a.costo_a_persona", currency_field="currency_id", string="Costo a persona", store=True)
Avatar
Discard
Best Answer

Hi,

The error may occurring because the currency field in b.py is now False.Try to assign a default currency in the field and try again or give ‘related’ to the currency field also(related=’b_a.currency_id’)

Regards

Avatar
Discard
Best Answer

Also if you wanted to use global company settings:

currency_id = fields.Many2one('res.currency', default=lambda self: self.env.company.currency_id, readonly=True)


If above does not work then below should work:


company_id = fields.Many2one('res.company', 'Company', required=True, default=lambda self: self.env.company)

currency_id = fields.Many2one('res.currency', related='company_id.currency_id', readonly=True)

Avatar
Discard