问题描述
Odoo 的导出功能支持对 tree
视图数据进行分组并导出数据。但如果导出分组中的某些数据被标记为 active=False
,则这些数据默认会被忽略。以确保在特定需求下,active=False
的数据也能够正确导出。
解决方案
以下是修改后的代码:
def base(self, data, token):
params = json.loads(data)
model, fields, ids, domain, import_compat = \
operator.itemgetter('model', 'fields', 'ids', 'domain', 'import_compat')(params)
Model = request.env[model].with_context(import_compat=import_compat, **params.get('context', {}))
if not Model._is_an_ordinary_table():
fields = [field for field in fields if field['name'] != 'id']
field_names = [f['name'] for f in fields]
if import_compat:
columns_headers = field_names
else:
columns_headers = [val['label'].strip() for val in fields]
groupby = params.get('groupby')
if not import_compat and groupby:
groupby_type = [Model._fields[x.split(':')[0]].type for x in groupby]
# 关键代码:检测 active=False 条件
has_active_false = False
for condition in domain:
if condition[0] == 'active' and condition[1] == '=' and condition[2] == False:
has_active_false = True
break
domain = [('id', 'in', ids)] if ids else domain
if has_active_false and ids:
# 如果检测到 active=False 条件且存在 ids,则将条件追加到 domain 中
domain.append(('active', '=', False))
groups_data = Model.read_group(domain, [x if x != '.id' else 'id' for x in field_names], groupby, lazy=False)
# 构建分组树结构
tree = GroupsTreeNode(Model, field_names, groupby, groupby_type)
for leaf in groups_data:
tree.insert_leaf(leaf)
response_data = self.from_group_data(fields, tree)
else:
records = Model.browse(ids) if ids else Model.search(domain, offset=0, limit=False, order=False)
export_data = records.export_data(field_names).get('datas',[])
response_data = self.from_data(columns_headers, export_data)
return response_data