Skip ke Konten
Menu
Pertanyaan ini telah diberikan tanda
2 Replies
6826 Tampilan


_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
Buang
Jawaban Terbai

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
Buang
Jawaban Terbai

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
Buang
Post Terkait Replies Tampilan Aktivitas
2
Mar 24
1946
1
Mei 17
3283
1
Feb 24
1652
2
Jan 24
1972
1
Mei 23
14360