How I can save the binary fields in the file system? (I am not referring to the attachments)


    I wish I could save the contents of binary field in the filesystem and in the database only one string reference type is keep.

   I did a debug in the source /web/controllers/main#Binary and I see that the files in Base64 encoded content is uploaded.

    I have also read here that it is possible save the attachment in the file system, but I do not mean it. I mean I want to keep all binary fields to the file system to prevent the tables from growing.

    Searching in I found the project I think that does something similar to what i want, but unfortunately, this module does not work in my version 8.0

    Would know me that way I can get my database did not grow disproportionately?

Thank you very much

1 Answer
Best Answer

I think this functionality describe above is quite interesting. Anyone else would think? Has anyone tried to implement it?

1 Comment
Fernando La Chica Mera

I create two field on my model like this: nombre_apk_desarrollo = fields.Char(string ="Nombre paquete desarrollo") apk_desarrollo = fields.Binary(compute='_data_get_desarrollo', inverse='_data_set_desarrollo', string=_('File content')) and this is two procedures called by computed field def _data_get_desarrollo(self): if not self.nombre_apk_desarrollo: return else: value_path = "" value_path = manejoficheros.busca_ruta_configuracion(); filename = value_path + self.nombre_apk_desarrollo try: r = open(filename, 'rb').read().encode('base64') self.apk_desarrollo = r except IOError: _logger.exception('_data_get_desarrollo reading: %s' % filename) def _data_set_desarrollo(self): if not self.nombre_apk_desarrollo: return else: self.cambiosRealizadosDes = False value_path = "" nombreFicheroAPK = self.nombre_apk_desarrollo extension = self.dime_extension_fichero(nombreFicheroAPK) if (extension[0] != "apk"): raise ValidationError('El fichero de desarrollo introducido no es un apk.') else: value_path = manejoficheros.busca_ruta_configuracion(); #self.busca_ruta_configuracion() filename = value_path + self.nombre_apk_desarrollo validacion = self.comprueba_si_existe_apk(True,nombreFicheroAPK) if (validacion[0] == True): raise ValidationError("El apk indicado ya está asignado a otra aplicación.") else: try: valoresApkDes = AplicacionTemp() with open(filename, 'wb') as fp: bin_value = self.apk_desarrollo.decode('base64') fp.write(bin_value) os.rename(filename,value_path + "des_" + self.nombre_apk_desarrollo) filename = value_path + "des_" + self.nombre_apk_desarrollo #self.nombre_apk_desarrollo = "des_" + nombreFicheroAPK valoresApkDes.nombre_apk_desarrollo = "des_" + nombreFicheroAPK infoAPK = self.dime_informacion_apk(True) #self.version_des = infoAPK[0][0][0] valoresApkDes.version_des = infoAPK[0][0][0] if ( == "" or == False): = infoAPK[0][0][1][0] = infoAPK[0][0][1][0] else: = if (self.imagen == None): path_icono= infoAPK[0][0][2] + "/res/drawable/icon.png" icono_base_64 = self._set_imagen_app(path_icono) #self.imagen = icono_base_64[0] valoresApkDes.imagen = icono_base_64[0] else: valoresApkDes.imagen = self.imagen #self.nombre_paquete_apk_prod = infoAPK[0][0][3] valoresApkDes.nombre_paquete_apk_des = infoAPK[0][0][3][0] #recojo los valores del AndroidManifest.xml para forzar la escritura de estos valores en base de datos en un solo objeto #de esta manera puedo controlar cuando ha terminado de realizarse todos los cálculos que provoca la subida del APK self.valoresApkDes = valoresApkDes self.nombre_apk_desarrollo = valoresApkDes.nombre_apk_desarrollo self.version_des = valoresApkDes.version_des = valoresApkDes.imagen = icono_base_64[0] self.cambiosRealizadosDes = True self.nombre_paquete_apk_des = valoresApkDes.nombre_paquete_apk_des self._elimina_carpeta_temporal(infoAPK[0][0][2]) except IOError: _logger.exception('_data_set_desarrollo writing: %s' % filename)