Community: Framework mailing list archives

expert-framework@mail.odoo.com

Re: v8.0 - Cannot create computed field - Field is accessed before being computed.

by
Pedro M. Baeza
- 10/16/2014 06:51:07
Hi, the problem is here:

if not  self.datas_fname:
    return

You must provide a value, although it can be False:

if not  self.datas_fname:
    self.data = False

Regards.



2014-10-16 12:47 GMT+02:00 Maxim Litnitskiy <litnimaxster@gmail.com>:
Hi Axel.
Thanks for your reply.
But I already do it.
Please look at this code.
This works on read and update (when I manually create a record in database), I can download file and I can change it.
And only gives error when I try to create a new record via Create button.

import logging
from openerp import fields, models, api, _

_logger = logging.getLogger(__name__)

class SoundFile(models.Model):
    _name = 'asterisk_dialer.sound_file'
    
    name = fields.Char(string=_('Sound file name'), required=True)
    datas = fields.Binary(compute='_data_get', inverse='_data_set', string=_('File content'), required=True)
    datas_fname = fields.Char(string=_('File name'), required=True)
    description = fields.Text(string=_('Description'))
    
    @api.one
    def _data_get(self):
        if not  self.datas_fname:
            return
            
        filename = '/tmp/%s' % self.datas_fname
        try:
            r = open(filename, 'rb').read().encode('base64')
            self.datas = r
        except IOError:
            _logger.exception('_data_get reading: %s' % filename)

        
    @api.one
    def _data_set(self):        
        filename = '/tmp/%s' % self.datas_fname
        try:
            with open(filename, 'wb') as fp:
                bin_value = self.datas.decode('base64')
                fp.write(bin_value)
        except IOError:
            _logger.exception('_data_set writing: %s' % filename)
                    
    

_______________________________________________
Mailing-List: https://www.odoo.com/groups/community-framework-62
Post to: mailto:expert-framework@mail.odoo.com
Unsubscribe: https://www.odoo.com/groups?unsubscribe