I have a problem with 2 modules in Odoo, the first is my auxiliary module that stores the price per kilo of each beef:
enter code herefrom odoo import api, fields, models
class LivestockPrice(models.Model):
_inherit = ['mail.thread', 'mail.activity.mixin']
_name = 'price.pound'
_description = "New pound price"
_order = 'date_pound desc'
name = fields.Char(string="New")
active = fields.Boolean(string="Active", default=True)
cow_id = fields.Many2one(
'livestock.model',
string="Beef identifier",
required=True
)
date_pound = fields.Date(
string="New price date",
required=True,
default=fields.Date.today()
)
price_import = fields.Monetary(
string="Price per kilo",
required=True,
tracking=True
)
type_cow = fields.Selection(
related='cow_id.type_cow',
string="Type of livestock",
readonly=False,
store=True,
required=True
)
What I want is to record prices per pound on a certain date and according to the type of cattle, that is, it stores several prices per pound according to the type_cow, now, this is my main model:
from odoo import api, fields, models
from odoo.exceptions import ValidationError
class LivestockModel(models.Model):
_name = 'livestock.model'
_inherit = ['mail.thread', 'mail.activity.mixin']
_description = 'New cattle from the agricultural and livestock farm created'
_order = 'date_create desc'
_rec_name = 'cow_id'
_order = 'cow_id asc'
cow_id = fields.Char(
required=True,
string="Beef identifier",
tracking=True
)
weight_cow = fields.Float(
string="Current weight",
tracking=True,
store=True
)
other_model = fields.Many2one(
'price.pound',
string="Price per pound"
)
price_actually = fields.Monetary(
compute="_compute_price_actually",
string="Current price per kilo",
store=True
)
price_import = fields.Monetary(
related="other_model.price_import",
string="Price per kilo",
store=True
)
price_total = fields.Monetary(
compute="_compute_cost_import",
string="Amount",
store=True
)
type_cow = fields.Selection(
[
('Steer', 'Steer of meat'),
('Heifer', 'Beef Heifer')
],
string="Type of livestock:",
required=True
)
@api.depends('weight_cow', 'other_model.price_import')
def _compute_cost_import(self):
"""This function calculates the amount of each beef"""
for record in self:
price_import_record = self.env['price.pound'].search(
[('type_cow', '=', record.type_cow)],
order='date_pound desc', limit=1
)
if record.weight_cow and price_import_record:
record.price_total = record.weight_cow * price_import_record.price_import
else:
record.price_total = 0.0
As you can see, in my livestock.model I use my own type_cow for that model, what I am looking for is to calculate the amount for each cow according to the type of livestock, which is type_cow, but I have a problem, it does it but only when I enter a new peso, which it shouldn't be, because it should be calculated automatically when I enter a new price per pound or a new peso. You must do the calculation whenever a new price_import or weight record is introduced in that model.
print()(andprint(type(...)),print(len(...)), etc.) to see which part of code is executed and what you really have in variables. It is called"print debugging"and it helps to see what code is really doing.