Skip to Content
Menú
This question has been flagged
2 Respostes
15158 Vistes

The error also gives a hint

"

HINT:  You will need to rewrite or cast the expression.

"

here's my python code:


class AccountInvoice(models.Model):
_inherit = 'account.invoice'

start_date = fields.Date()
end_date = fields.Date()
total_months = fields.Integer(compute='_total_months')
deferred_revenue = fields.Boolean()

@api.multi
def _total_months(self):
if self.start_date and self.end_date:
for rec in self:
date1 = dt.strptime(str(rec.start_date), '%Y-%m-%d')
date2 = dt.strptime(str(rec.end_date), '%Y-%m-%d')
r = rd(date2, date1)
rec.total_months = r.months

@api.multi
def action_invoice_open(self):
res = super(AccountInvoice, self).action_invoice_open()
obj = self.env['account.asset.category'].browse([('method_number', '=', self.total_months)]).id
if self.deferred_revenue == True:
self.env['account.asset.asset'].create({
'name' : self.name or 'kkkkk',
'category_id' : obj,
'date' : self.date,
'value' : self.amount_total,
'partner_id' : self.partner_id.id,
'invoice_id' : self.id,


})
return res

i want 'category_id' to get  a specified account from model 'account.asset.category' on the basis of total_months. 

In model  'account.asset.category' method_number is defined. for example if total_months = 5 then the category_id should be 'cat 5' from 'account.asset.category' model. i've tried different solutions. hope I have explained my problem.

Avatar
Descartar

@niyas Thank You so much it worked (y)

Don't forget to upvote/accept answers if they've helped you. I'll accept this one as I consider it solved your issue.

Best Answer

Hi,

You can browse the records by its ID not the condition, if you need to get the records based on condition use search,

obj = self.env['account.asset.category'].search([('method_number', '=', self.total_months)]).id

Once you search for the matching records, there is chance for multiple records which is matching same condition, so you can use limit=1 , to get a single record or iterate over the loop to get the ID based on the need.

obj = self.env['account.asset.category'].search([('method_number', '=', self.total_months)], limit=1).id

Thanks

Avatar
Descartar
Related Posts Respostes Vistes Activitat
2
de des. 17
4668
2
de jul. 24
3011
1
de juny 24
5441
1
d’oct. 23
11248
1
d’oct. 23
98