【问题标题】:What does context.get('active_ids', []) mean? Why do we use it?context.get('active_ids', []) 是什么意思?我们为什么用它?
【发布时间】:2018-08-29 07:05:32
【问题描述】:

以下代码来自Odoo 10中的export_stockinfo_xls。我想知道context.get('active_ids', [])是什么意思,返回值是什么。为什么我们在那个表达式中使用[]?

@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'
        }

【问题讨论】:

    标签: python odoo odoo-10


    【解决方案1】:

    如果您在树形视图中,context.get('active_ids', []) 返回已检查元素 ID 的列表。如果您在表单视图中,它会返回您在屏幕上看到的项目的 ID。 如果没有元素 id,则返回一个空列表 ([])。 希望对您有所帮助。

    【讨论】:

    • 能否详细说明“树视图”和“表单视图”?
    【解决方案2】:

    dict.get 方法用于从字典中获取值。它有一个额外的参数,可以从字典中返回一个默认值。

    dict.get 演示:

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

    输出:

    Hello, World!
    No Value
    

    MoreInfo

    就你而言。

    context.get('active_ids', [])
    

    如果'active_ids' 不在上下文中,则返回一个空列表。

    【讨论】:

      猜你喜欢
      • 2016-11-29
      • 1970-01-01
      • 2013-05-10
      • 2011-04-21
      • 1970-01-01
      • 1970-01-01
      • 2022-12-03
      • 2020-03-08
      • 2012-03-23
      相关资源
      最近更新 更多