跳至内容
菜单
此问题已终结

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):


 

形象
丢弃
最佳答案

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


形象
丢弃
编写者

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.

最佳答案

Try this:

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

形象
丢弃
编写者

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?

相关帖文 回复 查看 活动
1
3月 22
5629
1
11月 19
5238
0
7月 15
3911
2
6月 20
11503
2
11月 18
6193