Přejít na obsah
Menu
You need to be registered to interact with the community.
This question has been flagged
2 Odpovědi
6704 Zobrazení


_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
Zrušit
Nejlepší odpověď

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
Zrušit
Nejlepší odpověď

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
Zrušit
Related Posts Odpovědi Zobrazení Aktivita
2
bře 24
1835
1
kvě 17
3213
1
úno 24
1595
2
led 24
1918
1
kvě 23
14294