Skip to Content
Menu
This question has been flagged
2 Replies
6733 Views


_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
Discard
Best Answer

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
Discard
Best Answer

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
Discard
Related Posts Replies Views Activity
2
Mar 24
1864
1
May 17
3234
1
Feb 24
1608
2
Jan 24
1932
1
May 23
14319