Overslaan naar inhoud
Menu
Je moet geregistreerd zijn om te kunnen communiceren met de community.
Deze vraag is gerapporteerd
2 Antwoorden
14539 Weergaven

I'm trying to create a XML file and add it as a attachment to the record. It generates the xml as a string and saves it into 'ir.attachment' but I don't know how to add it to the models record so I could download it from the custom modules form view.

    @api.model

def attach_xml_file(self, docids):

import base64

xml_string = self.generate_xml()

file_name = 'myfile-1.xml'

attach_name = file_name

attach_id = self.env['ir.attachment'].create({'name': attach_name,

'datas': base64.encodestring(xml_string),

'datas_fname': file_name,

'res_model': 'this.model.name',

'res_id': docids[0]})

Do I have to inherit the ir.attchment in my custom model? How can I attach the file to the specific instance of the model? Is there a odoo base addon that does this and I could use it as example? 



Avatar
Annuleer
Auteur Beste antwoord

OK did it. used etree from lxml to create the file and convert it to string with etree.tostring() and then encoded the string with base64 and passed it to a field.

self.xml_doc = base64.encodestring(etree.tostring(xml_root, pretty_print=False, encoding='utf8'))
Avatar
Annuleer
Beste antwoord

Hello Samo Arko

You can Try This, 

attachment = {'name': 'Name of Attachmnet',    
              'datas': base64.encodestring('Data'),
             'datas_fname': 'File Name',
             'res_model': 'Model Name',
             'res_id': 'res_id'}

self.env['ir.attachment'].create(attachment)
Avatar
Annuleer
Gerelateerde posts Antwoorden Weergaven Activiteit
2
jul. 25
4356
2
dec. 24
7579
2
nov. 24
28328
2
mei 24
7318
3
mrt. 24
6748