Ir al contenido
Menú
Se marcó esta pregunta
2 Respuestas
1026 Vistas

I have this code, I am trying  to divide the total price by 6 and display that under the total price. No matter what I do it's not working - it looks to me like Odoo is ignoring the code altogether or something

from odoo import fields, models

class Expenses(models.Model):
_name = 'expenses.model'

total_price = fields.Float(string='Total Price')
divided_amount = fields.Float(string='Divided Amount', compute='_compute_divided_amount', store=True)

@api.depends('total_price')
def _compute_divided_amount(self):
for expense in self:
expense.divided_amount = expense.total_price / 6

Avatar
Descartar
Autor

thank you for this, I added api as well but I am not sure about the _init_file, there are two: one under the models folder which is empty and one on the root folder with this:

from . import models


is that correct?

yes, in the file under models directory you have to import your python file just like init file of root folder

from . import your_python_file_in_models

Mejor respuesta

Hi,

In your code missing the import statement for api from the odoo module 

(from odoo import fields, models, api)

Apart from this, the code you have shared seems fine. So please make sure that you have imported your python file in  __init__ file , ensure that you have added the correct dependencies in your module's manifest file (__manifest__.py) to make Odoo aware of the dependencies and properly load your code.

Also please check the function is executing after changing the value in your depends field 'total_price'


Hope this will help you

Thanks

Avatar
Descartar
Autor Mejor respuesta

I tried to do it but it's not working, even tried to add the code to the system .py file and it's not doing it. How can I just divide the total number by 6 and display that? It's not the simple total but the one below:

Unit Price?£1

Quantity?50

£ 50.00 --> this field have no name and no (?) next to it, to see the name of it. that's the one I need to divide and then display it on another field called x_davide that is monetary

Avatar
Descartar
Autor

I found that I can add compute lines on the customs created fields but still not working. Even this is not working:

for expense in self:
expense.x_divide = expense.total_amount