Skip to Content
मेन्यू
This question has been flagged
2 Replies
6710 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
मार्च 24
1838
1
मई 17
3215
1
फ़र॰ 24
1599
2
जन॰ 24
1925
1
मई 23
14296