I have created a module and a calculated field that allows you to retrieve a value from the database. Since the field is calculated I can't use the field to make conditions. I get the error "Non-stored field rdv.rdv.nb_rdv cannot be searched.". To solve the problem I want to store the value of the field using store=True but the default value is 0 and doesn't change.
This is my fields:
projet_terminer_id = fields.Many2one('op.course',string='Projet', required = True )
name_student_id = fields.Many2one('res.users', 'Etudiant', required=True )
date_rdv = fields.Datetime('Date de RDV', required=True)
demandeur_rdv = fields.Integer('Demandeur de RDV', default=lambda self: self.env.uid, readonly=True )
nb_rdv = fields.Integer(string='Nombre de rdv', compute="nombre_de_rdv", readonly=True)
test_field = fields.Integer('Champ de Test')
This my function:
@api.multi
@api.depends('nb_rdv')
def nombre_de_rdv(self):
for record in self:
id_student = record.name_student_id
id_course = record.projet_terminer_id
nb = self.env['rdv.rdv'].sudo().search_count([('name_student_id','=', id_student.id), ('projet_terminer_id', '=', id_course.id)])
record.nb_rdv = int(nb)
Thanks to help me.
In the decorator depends you should lists the fields that are used to compute the field, and In that way odoo will monitor this fields when ever they are changed the field will be recomputed. By the look of your code I think this will do it
@api.depends('name_student_id', 'projet_terminer_id')
Note when you make the field stored Odoo will compute the values of Old records when you upgrade the module but will not do in thext next one, this operation is done only one time. If you have old records you should make the field store=False
(write it like this do not just remove store=True
) upgrade the module then make it store again and upgrade the module. But for new record you will notice that when ever you change the value of one of the fields the value will be recomputed