Skip to Content
Menu
This question has been flagged
2700 Views

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()
field_float4 = fields.Float()
field_char = fields.Char()

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.

I've tried this solution:

@api.depends ('one_1', 'one_2','one_1.field1', 'one_2.field2') 
     def compute_sum(self):

And add compute_sum as a compute="" into Model1,

one_3 = fields.One2many('Model4', 'field_many3', string='', compute="compute_sum")

but this doesn't works, as it makes all the line readonly and expecting the sum of other two fields/models.


Which doesn't works because 'one_3' it has also some other fields which shouldn't be computable.

Any ideas?




Avatar
Discard
Related Posts Replies Views Activity
3
Jul 16
18714
0
Apr 24
401
4
Nov 23
4251
0
Oct 23
378
0
Dec 22
1334