Skip to Content
Menu
This question has been flagged
5 Replies
8360 Views
I wish to know how to add values ​​automatically from one class to another.

and if I change the value automatically change in another

for example I have a class limit

class DiningRoomLimit(models.Model):

    _name = 'dining.room.limit'
    _order = 'sequence'

    limit = fields.Monetary(string='Limite de Credito', required=True)
    

y otra descuento

class DiningRoomDiscount(models.Model):

    _name = 'dining.room.discount'

    prueba = fields.Monetary(string='Base imponible',  )
  


and I want that when entering a value in limit, I automatically update the test value that is in another class
Avatar
Discard
Best Answer

Hi,

You can write a function in the onchange method of the field to write the value of a record in another model.

for eg:

@api.onchange('limit')
def onchange_limit(self):
record = self.env['dining.room.discount'].browse(YOUR RECORD ID) record.write({'prueba': YOUR VALUE})

Thank you.

Avatar
Discard
Author Best Answer
thanks for the method avinash the method that I am using in the following:

class DiningRoomLimit(models.Model):

 _name = 'dining.room.limit'

 _order = 'sequence'


 name = fields.Char(readonly=True, create="false", store=True)

 limit = fields.Float(string='Limite de Credito', required=True, digits=(64, 2)) 

@api.onchange('limit')

 def _onchange_limit(self):

  h = j = 0.0

  for r in self:

   h = r.limit

   j = r.limit - h

   result = self.env['hr.employee'].search([])

   result.write({

    'limit_id': j,

    })

class DiningRoomIndexCord(models.Model):

 _rec_name = 'ficha'

 _inherit = ['hr.employee']

 

 limit_id = fields.Float(string='Limite de Credito', required=True, digits=(64, 2) )



I am trying to perform a subtraction between the limit and the limit_id that is in the class hr.employee and this result is the one that is saved in limit_id

Avatar
Discard
Best Answer

You Can use _compute

@api.depends('limit')
    def _limit_prueba(self):
            if self.limit:
                prueba_obj = self.env['dining.room.discount'].search([])

                 for prueba_field in prueba_obj:
                     prueba_field.prueba = self.limit

limit = fields.Monetary(string='Limite de Credito', required=True, _compute='def _limit_prueba')


You Can apply some domain in search as per Your convenience.


Avatar
Discard

If you run this code all records in model "dining.room.discount" will be having same value. Its better to use browse instead of search. Also its far better to use onchange as Avinash NK suggested, than using compute in this situation

Best Answer

If you want to pass in the same view you have to use context, I mean, if you have a view with links the model A with model B in the same form you can pass the value of field in model A to field in model B using context in the field declaration in xml. Otherwise you can create an @api.depends or @api.onchange decorated method who do the copy of value you want.

 you can use the methods in the previous posts to achieve that, make some tweaks depending on your version of Odoo and add a domain if you consider necessary, seems you want to add a different discount for every single dinningRoom so I think a domain is necessary in this case.

Avatar
Discard