Ir al contenido
Menú
Se marcó esta pregunta
2 Respuestas
6626 Vistas


_inherit = 'res.partner'
staff_code = fields.Char(compute='_random_code', string='Code nhân viên', index=True)
@api.multi
def _random_code(self):
if not self.staff_code:
chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
code = ''.join(random.SystemRandom().choice(chars) for i in xrange(8))
full_code = 'NV-' + code
self.staff_code = full_code

If dont use "store=True" staff_code change continuously when view user infomation.
If use "store=True", i can't install module.
Many thank!


Avatar
Descartar
Mejor respuesta

I had the same problem some time ago, the only solution that I found was overwrite the create method to calculate only one time.

Without computed field, something like that:

@api.model

def create(self, vals):    
    if not self.staff_code:        
        test = super(YourClassName, self).create(vals)        
        chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'        
        code = ''.join(random.SystemRandom().choice(chars) for i in xrange(8))        
        full_code = 'NV-' + code        
        test.staff_code = full_code        
        return test

Avatar
Descartar
Mejor respuesta

Hi,

Either you have to use store=True or store=False. What is the issue that you are getting on installing the module if store=True is given ?

Then if you didn't use store=True, the value will be computed on the fly based on the given condition in the function. Data is changing continuously means , is it get computed always or the value that function returning is always is different ? If second is the case, it depends on your function and given logic.

1. Why Stored Computed Fields Are Not Recomputing in Odoo

2. How to Write Compute Field and its Function in Odoo12

Thanks

Avatar
Descartar
Publicaciones relacionadas Respuestas Vistas Actividad
2
mar 24
1764
1
may 17
3165
1
feb 24
1530
2
ene 24
1848
1
may 23
14201