1

I have 2 methods in method1:

def print_xls(self):
    record = self.env['sale.order'].search([('id','in',filtered_list)])
    data = {
        
        'model_recs':record
    }
    return self.env.ref('module_name.report_name').report_action(self, data=data)

in method 2:

def generate_xlsx_report(self, workbook, data, lines):
    h1 = workbook.add_format({'font_size': 16, 'align': 'center', 'align':'center','valign':'vcenter', 'bold': True,'underline':True})
    sheet = workbook.add_worksheet('sale dept')
    sheet.merge_range(1, 0, 3, 17,"Sale Report",h1)
    print(data['model_recs'])

I am getting data['model_recs'] = 'sale.order(1,2,3)'

My question is how to convert the string model 'sale.order(1,2,3)' into Model/object sale.order(1,2,3), so the i can get it's field data like sale.order[0].some_field in second method.

4
  • There is something missing here. You flll data in the first part, but don't use it in the second. Can you add that please? Commented Jul 30, 2021 at 8:15
  • That should be normal recordsets (objects), but the string representation is like you're saying. Did you try to loop on them? Commented Jul 30, 2021 at 9:55
  • @CZoellner cannot loop over string 'sale.order(1,2,3)'. that's why I want to convert that string into record set/model. Commented Jul 30, 2021 at 10:19
  • okay, that's weird. Commented Jul 30, 2021 at 11:17

1 Answer 1

0

You can use eval("sale.order(1,2,3)")

>>> eval("str(1)")
'1'
>>> eval("type(str(1))")
<class 'str'>

>>> class x:
...     def gg(self, x, y, z):
...             print(x, y, z)
... 
>>> eval('x().gg(1,2,3)')
1 2 3

From here https://stackoverflow.com/a/42227653/7035448

While it is very powerful method it is also worthwhile to note problems with eval eval_problems

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.