我想将导入/导出操作提供给odoo 10中的自定义按钮,位于csv文件或excel文件中。 怎么做?
谢谢
答案 0 :(得分:2)
单击按钮导出时调用的方法是export_data
您可以在/odoo/odoo/models.py文件夹中查看models.py文件中的代码
@api.multi
def export_data(self, fields_to_export, raw_data=False):
""" Export fields for selected objects
:param fields_to_export: list of fields
:param raw_data: True to return value in native Python type
:rtype: dictionary with a *datas* matrix
This method is used when exporting data via client menu
"""
fields_to_export = map(fix_import_export_id_paths, fields_to_export)
if raw_data:
self = self.with_context(export_raw_data=True)
return {'datas': self._export_rows(fields_to_export)}
我不尝试,但我认为你必须这样做
@api.multi
def execute(self):
model = "name_of_your_model_want_export"
export_datas = self.env[model].export_data(['field1', 'field2'])
# code to write to a file CSV of EXCEL
进口。您必须使用模型的create方法。
@api.multi
def execute(self):
# This function return list of dict like this
# [{'field1': 'Value1','field2': 'value2}, {'field1': 'Value1','field2': 'value2}]
datas = data_from_your_csv(self.file)
model = "name_of_your_model_want_import"
for data in datas:
record = self.env[model].create(data)