I'm trying to get the ID of the currency in a Journal Item but I get a False boolean value for some reason.
Here's the relevant code:
class JournalItem(models.Model):
_name = "account.move.line"
_inherit = "account.move.line"
def _get_current_rate(self):
...
print(self.currency_id.id)
#currency_id is a Many2one field of comodel res.currency.
rate = fields.Float(string="Rate", digits=(12, 6),
default=_get_current_rate)
Which outputs:
False
I toyed around with the @api.multi and @api.one decorators but noticed no changes. Feels like I'm missing something crucial.
Update 1:
self
is account.move.line()
when checked
This should be like account.move.line(1, 2, 3, 4, ...)
maybe it's something to do with the default attribute, will update as I figure it out.
Update 2:
The main thing here is the lack of a currency, it persists even when given a valid Journal Entry record number
From the 'account' module:
@api.model
def _get_currency(self):
currency = False
context = self._context or {}
if context.get('default_journal_id', False):
currency = self.env['account.journal'].browse(context['default_journal_id']).currency_id
return currency
This is unlikely to be the issue since currency_id returns a res.currency object, it's only when I further look into it that I get False return values.
Update 3:
It appears that currencies not explicitly set during transaction creation are set to False, this is likely the source of my troubles. Fixed by testing for it and replacing by environment's default.
Methods for default values always have an empty recordset. You should fill the value in one of the following ways:
compute the value and store it (little hint: call the currency field rate
with the journal entries date in context --> currency.with_context(date=<journal_entry_date>).rate
--> because this field is computed and context date is taken into the computation)
override create/write and try to figure out the rate