4

The following code is from export_stockinfo_xls in Odoo 10. I want to know what mean context.get('active_ids', []) and what is the returned value. Why do we use [] in that expression?

@api.multi
def export_xls(self):
    context = self._context
    datas = {'ids': context.get('active_ids', [])} # what mean this and what return

    datas['model'] = 'product.product'
    datas['form'] = self.read()[0]

    for field in datas['form'].keys():
        if isinstance(datas['form'][field], tuple):
            datas['form'][field] = datas['form'][field][0]
    if context.get('xls_export'):
        return {
            'type': 'ir.actions.report.xml',
            'report_name': 'export_stockinfo_xls.stock_report_xls.xlsx',
            'datas': datas,
            'name': 'Current Stock'
        }
ChesuCR
  • 9,352
  • 5
  • 51
  • 114
MOHAMED
  • 101
  • 2
  • 10

2 Answers2

4

If you are in a tree view context.get('active_ids', []) returns a list of checked elements ids. If you are in a form view it returns the id of the item that you see on the screen. If there is no element id it returns an empty list ([]). I hope this help you.

Dayana
  • 1,500
  • 1
  • 16
  • 29
2

the method dict.get is used to fetch a value from a dictionary. It has an additional parameter that returns a default value from a dict.

dict.get Demo:

dictionary = {"message": "Hello, World!"}
print(dictionary.get("message", ""))
print(dictionary.get("message_1111", "No Value"))   #Note: "message_1111" not in dictionary. 

Output:

Hello, World!
No Value

MoreInfo

In your case.

context.get('active_ids', [])

if 'active_ids' is not in context returns an empty list.

Rakesh
  • 81,458
  • 17
  • 76
  • 113