This question has been flagged
1 Reply
9323 Views

how to get  one2many field record value in xlsx report.

wizard.py

class TimesheetReportWizard(models.TransientModel):
_name = 'timesheet.report.wizard'


employee = fields.Many2many("hr.employee", )
period = fields.Selection([('day', 'Day(s)'), ('month', 'Month')], default='day')
from_date = fields.Date()
to_date = fields.Date()

wizard.xml
<button name="generated_excel_report1" string="Export to excel" type="object" class="btn-primary"/>

onother_model.py
_name = 'timesheet.timesheet'

sheet_ids=one2many()
here sheet_ids has the field date,item,hours

My question is how to get sheet_ids=one2many() field value to the wizard(report)


please give some ideas to me , how to do this,


Avatar
Discard
Best Answer

Hello Ajini,

i am not perfectly able to help you, but i can help you how to create XLS report in odoo and set into binary field that you can download. Just follow bellow step.

# import packages required for writing XLS data

from io import BytesIO

import xlwt,  base64


# Define one field that temporary hold data for download

file = fields.Binary('XLS File', readonly=True)


# Do bellow stuff in onchange / method or button click function to write XLS  

# set sheet name and format

FP = BytesIO()

WB = xlwt.Workbook(encoding='utf-8')

writer = WB.add_sheet('Sheet Name')


row = 1

col = 0

header_cols = 4


# Set sheet first row (for header purpuse)  

writer.write_merge(row, row, 0, header_cols, "Contacts information", "you can apply css style here for fiest column like: height: 200px;")


row += 1


# write data (for column title)

writer.write(row, col + 1, "Name", "font-weight:bold;")

writer.write(row, col + 2, "Email", "font-weight:bold;")

writer.write(row, col + 3, "Phone", "font-weight:bold;")


row += 1

col = 0


# Set data

writer.write(row, col + 1, "A", "font-weight:normal;")

writer.write(row, col + 2, "B", "font-weight:normal;")

writer.write(row, col + 3, "C", "font-weight:normal;")


WB.save(FP)


# set file to binary field

self.file = base64.encodestring(FP.getvalue())


Hope it will help you to do what you want.
I think you have to add this code to loop for printing One2many data.

Accept and upvote answer if helpful

Thanks and regards 
Haresh Kansara
Avatar
Discard

Notice that base64.encodebytes(FP.getvalue()) is adviced for Python 3 over base64.encodestring(FP.getvalue())