1
votes

I have two odoo 12 models,(biblio.location and biblio.book), -the model "biblio.book" contains a boolean "disponibile" set to true by defaul. -the model "biblio.location" have a many2one field references to model "biblio.book".

i want the value of the boolean "disponible" in biblio.book to be changed automatically (change also in database) when a new instance of biblio.location is created, in other way when we rent(location) a book we must change disponibility in model book to FALSE.

i tried "computed field, @api.onchange and @api.depends" and nothing works for me, please help me in this issue and i want to know de difference between those three mehods.thank you

class book(models.Model):

_name = 'biblio.book'
_description = 'All books'
name=fields.Char()
annee_edition = fields.Date(string="année d'édition")
ISBN = fields.Integer(required=True)
editor = fields.Char(required=True)
auteur = fields.Many2many('biblio.author',string='auteur_livre',required=True)

disponible=fields.Boolean(default=True,required=True,related='biblio.location.disponible',store=True )

class location(models.Model):

_name = 'biblio.location'
_description = 'All librarians'

name=fields.Char()
livre = fields.Many2one('biblio.book',string='livre',required=True,domain =[('disponible','=',True)])
client = fields.Many2one('biblio.customer',string="client",required=True)
date_location =fields.Datetime(required=True)
date_retour_prevu=fields.Datetime(required=True,string="Date retour prévu")
date_retour_reelle=fields.Datetime(required=True,string="Date retour réelle")

disponible = fields.Boolean(default=False)

File "C:\Users\PycharmProjects\Odoo12\odoo\odoo\fields.py", line 484, in setup_full self._setup_related_full(model) File "C:\User\PycharmProjects\Odoo12\odoo\odoo\fields.py", line 527, in _setup_related_full field = target._fields[name] KeyError: 'biblio' - - -

1

1 Answers

0
votes

OK, for this to work the way you want it you need to set up a foreign key in your biblio.book model.

book_location = fields.Many2one('biblio.location', string='Book Location')

Then you can do your computed field

disponible = field.Boolean(compute='_disponible', string='Available for Loan', default=False)


@api.model
def _disponible(self):
    for book in self:
        book.disponible = True if book.book_location else False

You don't want to set this as storable as you want it to check every time that the field is called. If you set it to storable it will only compute when the record is created.