Skip to Content
Menu
This question has been flagged

How can I access all the files from attachment_ids in my method parse_xml?

I want to process the files.


class XMLImporter(models.Model):

    _name = 'ia.xml.importer' 

    _description = 'Helper class to import xml files'

    attachment_ids = fields.Many2many('ir.attachment', 'class_ir_attachments_rel', 'class_id', 'attachment_id', 'Attachments')


@api.multi

def parse_xml(self):


 

Avatar
Discard
Best Answer

Hope this helps you

attachment_ids = fields.Many2many('ir.attachment', 'class_ir_attachments_rel', 'class_id', 'attachment_id', 'Attachments')
@api.multi
def your_button_click(self):
for attachment in self.attachment_ids:
    decoded_data = base64.b64decode(attachment)
    # you logic goes here


Avatar
Discard
Author

thanks for your post.

this works:

for xml_file in self.attachment_ids:

decoded_data = base64.b64decode(xml_file.datas)

root = ElementTree.fromstring(decoded_data)

Confirmed that you need to use the ".datas" property to access the actual file binary data.

Best Answer

Try this:

for att in self.attachment_ids:
    xml = att.datas.decode('base64')
    xml_filename = att.datas_fname

Avatar
Discard
Author

thanks. I tried but does not work... LookupError: 'base64' is not a text encoding; use codecs.decode() to handle arbitrary codecs

for xml_file in self.attachment_ids:

deco_data = xml_file.datas.decode('base64')

tree = ElementTree.parse(deco_data)

root = tree.getroot()

do u know why?

Related Posts Replies Views Activity
1
Mar 22
5230
1
Nov 19
4828
0
Jul 15
3594
2
Jun 20
10994
2
Nov 18
5806