Reputation: 3897
Suppose I have:
class Model1(models.Model):
name = fields.Char(string="Name")
one_1 = fields.One2many('Model2', 'field_many', string="")
one_2 = fields.One2many('Model3', 'field_many2', string="")
one_3 = fields.One2many('Model4', 'field_many3', string="")
These are the models called from One2many
fields:
class Model2(models.Model):
field_many = fields.Many2one('Model1', string="")
field_float1 = fields.Float()
class Model3(models.Model):
field_many2 = fields.Many2one('Model1', string="")
field_float2 = fields.Float()
class Model4(models.Model):
field_many3 = fields.Many2one('Model1', string="")
field_float3 = fields.Float()
I need to sum field_float1
and field_float2
and show the result on field_float3
, but the problem is, or what gets me confused for what matters, is the fact that this sum isn't just a sum in the same model.
I could do:
@api.onchange('field_float1', 'field_float2')
def _compute_amount_move_sales_current(self):
if self.field_float1 or self.field_float2:
self.field_float3 = self.field_float1 + self.field_float2
But these are fields from two models, which result should be reflected on third model field, also, these models are represented on Model1
form, through 3 One2many
tree view, or lines as they are often called into Odoo.
So, it's just about to read the first two fields, but it must be on the form Model1
which will create the new record. Not the db table.
EDIT
This is a more precise explanation, a user creates a new record with these lines or One2many
fields, in all cases the first two must be filled, so I think that can be achieved with required=True
flag on the fields.
However, I've tried this way:
class Model1(models.Model):
name = fields.Char(string="Name")
one_1 = fields.One2many('Model2', 'field_many', string="")
one_2 = fields.One2many('Model3', 'field_many2', string="")
one_3 = fields.One2many('Model4', 'field_many3', string="", onchange="compute_sum")
@api.onchange('one_1', 'one_2')
def compute_sum(self):
if self.one_1.field_float1 or self.one_2.field_float2:
self.one_3.field_float3 = self.one_1.field_float1 + self.one_2.field_float2
But it doesn't work, whenever I fill one of the required field_float it says:
ValueError
Expected singleton: Model4()
For further explanation, the Models look like this:
class Model2(models.Model):
field_many = fields.Many2one('Model1', string="")
field_float1 = fields.Float()
field_float_ = fields.Float()
field_integer = fields.Integer()
field_char = fields.Char()
class Model3(models.Model):
field_many2 = fields.Many2one('Model1', string="")
field_float2 = fields.Float()
integer = fields.Integer()
class Model4(models.Model):
field_many3 = fields.Many2one('Model1', string="")
field_float3 = fields.Float()
chars = fields.Char()
bool = fields.Boolean()
I mean, there are several fields on each of these models, which are called from Model1
with One2many
fields, just those floats need the operation, not every one of them.
And the user will be filling these fields in order, with a state
like form, with statusbar
etc, in order.
Upvotes: 2
Views: 827
Reputation: 14721
This solution work if you want to sum
field1 of record1 In first one2many field with field2 of record1 In second one2many field to put the the result in field3 of the first record.
It's a start to understand what you need exactly. In your model1 makr third one2many field a compute field
@api.depends ('one_1', 'one_2','one_1.field1', 'one_2.field2')
def compute_sum(self):
# Then loop throw your records and compute sum
I would write more cde for you but i'm using my phone i may edit my answer later hope at least you get the idea.
If you use onchange it may work too when user change the value of the float field but remove one_1.field1 if you use it.
Just remember you need to define the method in yout model1 becausr it's there where you have full access
Upvotes: 2