Help

1

read and get file from ir.attachement

Avatar
ai

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
2 Answers
1
Avatar
Sehrish
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


1 Comment
Avatar
Discard
Avatar
ai
-

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)

0
Avatar
Zbik
Best Answer

Try this:

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

1 Comment
Avatar
Discard
Avatar
ai
-

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?