2017-07-21 1 views
3

Je souhaite enregistrer automatiquement une variable dans un fichier fieldss.binary (fichier_fichier_email) à partir de la fonction get_pdf.Enregistrement de la variable get_pdf() dans fields.binary

Mes codes ci-dessous ici:

class example_example(models.Model): 
    email_attachment_file = fields.Binary('Data (.txt,.pdf)') 
    email_filename   = fields.Char('Filename') 

    def generate(self,etc..): 
     report_name = "report_name_template" 

     datas = { 
      'ids':[], 
      'model' : etc, 
      'form' : etc 
      'context': context 
      } 

     moddelReport = self.pool.get('report') 
     alpha = modelReport.get_pdf(cr, uid,[],report_name,None,datas,context=context) 

     #alpha = base64.decodestring(alpha) 
     #alpha = alpha.decode('unicode_escape').encode('utf-8') 

     # --------- how to save alpha variable into fields.binary 

Et, est-il fonction modelReport.get_pdf quelque chose de mal?

Répondre

2

Utilisez encodestring() au lieu de decodestring().

report_obj = self.pool.get('report') 
data = modelReport.get_pdf(cr, uid,[],report_name,None,datas,context=context) 
self.email_attachment_file = base64.encodestring(data) 
+0

le retour de données% PDF-1,3 1 0 obj << /Enfants [] /Type/Pages /0 A >> endobj 2 0 obj << /Producteur (Python PDF Library \ 055 http \ 072 \ 057 \ 057pybrary \ 056net \ 057pyPdf \ 057) .. etc ..., je n'arrive pas à trouver la solution –

+0

get_pdf retourne le rapport sous forme de chaîne vous devez le convertir dans le binaire en utilisant base64.encodestring (data). –