This question has been flagged
2 Replies
3208 Views

I have a model in which i want that a field of one Entity depends on a field of another entity. Entities are bound with one2many/many2one relationship. I tried the following code but without success

I tried to follow official documentation example here: https://www.odoo.com/documentation/11.0/howtos/backend.html#dependencies but since code isn't complete I have difficulty in guessing variables

I also found some examples with @api.one and @api.multi and I would like to know the difference with @api.depends


class Cantiere(models.Model):

_name = 'modulotest01.cantiere'

name = fields.Char(required=True)

description = fields.Text()

magazzino = fields.One2many('modulotest01.magazzino', 'cantiere')

magazzinopieno = fields.Boolean(compute="_capacityMagazzino", store=True)

@api.depends('capacitaLotti', 'modulotest01.magazzino')

def _capacityMagazzino(self):

if self.capacitaLotti > 24:

magazzinopieno = True

class Magazzino(models.Model):

_name = 'modulotest01.magazzino'

capacitaLotti = fields.Integer()

cantiere = fields.Many2one('modulotest01.cantiere', string='Cantiere', index=True, ondelete='cascade' )

 

Avatar
Discard
Author

i tried with

capacitaMagazzini = fields.Integer('Magazzino Pieno', related='magazzino.capacitaLotti')

but in this way i get only the capacitaLotti of the first magazzino in the list

Best Answer

Try  self.magazzinopieno = True instead of magazzinopieno = True to save the value of magazzinopieno


def _capacityMagazzino(self):

if self.capacitaLotti > 24:

self.magazzinopieno = True

Avatar
Discard
Author

Thank you for your answer

I got this: KeyError: 'capacitaLotti'