OpenERP - 将数据复制到不同表中的新记录 - 费用/采购订单

时间:2014-11-03 09:44:06

标签: python python-2.7 openerp openerp-7 odoo

我一直在尝试将hr_expenses表单中的某些字段复制到我的自定义模块purchase_orders中。

以下方法是为我在扩展的hr_expenses.py文件中执行此操作的:

def po_generator(self, cr, uid, ids, arg, context=None):
            res = {}

            for expense in self.browse(cr, uid, ids, context=context):
                expense_line = self.pool.get('hr.expense.line')
                purchase_orders = self.pool.get('purchase.orders')

                for line in expense.line_ids:
                    expense_line_record = expense_line.search(cr, uid, [('id', '=', line.id)], context=context)
                    # pdb.set_trace()
                    purchase_orders.create(cr, uid,
                        {
                            'submitted_employee_name':expense.employee_id,
                            'dateofexpense':expense_line.date_value,
                            'project_id_expense':expense_line.project_id,
                            'Description':expense_line.name,
                            'netamount':expense_line.product_exc_VAT,
                            'raised_employee_name':expense.employee_id,
                            'project_id_account_type':expense_line.project_id_account_type,
                        },
                        context
                    )

            return res  

但是,当我执行这段代码时,OpenERP给我一个跟踪的'AttributeError':

  

文件   “/opt/openerp/custom_modules/legacy_expense_po/legacy_expense.py”   第121行,在po_generator中       'dateofexpense':expense_line.date_value,AttributeError:'hr.expense.line'对象没有属性'date_value'

我认为这与费用表单中的line_ids字段有关。我正在尝试获取line_ids中的每一行,并将其作为purchase_orders表中的新记录输入。

感谢。

1 个答案:

答案 0 :(得分:1)

@Sagar Mohan

你在这里做的各种基本错误。

您已经通过声明

来循环浏览记录
 for line in expense.line_ids:

当你说expense.line_ids这已经拉出所有相关的o2m记录时,你不需要再写一下expense_line_record = expense_line.search(cr, uid, [('id', '=', line.id)], context=context)语句,你必须知道v7中的搜索只返回匹配ID不是recordsetbrowse record。 正确的代码是:

 def po_generator(self, cr, uid, ids, arg, context=None):
        res = {}
        purchase_orders = self.pool.get('purchase.orders')
        for expense in self.browse(cr, uid, ids, context=context):
            expense_line = self.pool.get('hr.expense.line')
            purchase_orders = self.pool.get('purchase.orders')
            for line in expense.line_ids:
                purchase_orders.create(cr, uid,
                    {
                        'submitted_employee_name':line.employee_id.name,
                        'dateofexpense': line.date_value,
                        'project_id_expense': line.project_id.id,
                        'Description': line.name,
                        'netamount': line.product_exc_VAT,
                        'raised_employee_name': expense.employee_id.name,
                        'project_id_account_type': line.project_id_account_type,
                    },
                    context
                )
        return res

此代码可能需要根据您的需要进行更正,但您会看到该行是直接浏览变量,因此您只需要。并进入该领域。

还有一点请注意,你永远不会在内部循环中使用self.pool.get来使你的代码变重。

由于