在OpenERP ver 7中覆盖具有多个返回的create方法

时间:2013-04-29 07:39:29

标签: python xml openerp

我需要在我的activity方法中做两个活动。 一个是需要为total_workers字段返回值。但是还需要返回ID,因为当我们覆盖该方法时,它在OpenERP中是必需的。 然后必须返回2件事。请告诉我如何在我的表单中实现这一点。 有一个返回值,这适用于两件事。但需要返回两者。

def create(self, cr, uid, values, context=None):
    name = 'CheckRoll No = ' + str(values['checkroll_no']) + ' & Gang No = ' + str(values['gang_no'])
    values.update({'name': name})
    total_tea_workers = 0
    offer_id = super(bpl_work_offer, self).create(cr, uid, values, context=context)
    tea_worker_obj = self.browse(cr, uid, offer_id, context=context) or []
    tea_worker_ids = self.pool.get('bpl.selected.tea.workers.line').search(cr, uid, [('tea_line_worker_id', '=', tea_worker_obj.id)])
    for tea_worker in self.pool.get('bpl.selected.tea.workers.line').browse(cr, uid, tea_worker_ids):
        if tea_worker.is_selected:
            total_tea_workers += 1
    return {'value': {'total_workers': total_tea_workers}}
    return offer_id

EDITED

我以这种方式解决这个问题。这对其他人有帮助 : - )

  

我为我的字段编写了一个函数

def _compute_workers(self, cr, uid, ids, fieldname, arg, context=None):
    total_workers = total_tea_workers = total_rubber_workers = total_sundry_workers = total_other_workers = 0
    res = dict.fromkeys(ids, False) 
    for this in self.browse(cr, uid, ids, context=context):
        tea_worker_ids = self.pool.get('bpl.selected.tea.workers.line').search(cr, uid, [('tea_line_worker_id', '=', this.id)])
        for tea_worker in self.pool.get('bpl.selected.tea.workers.line').browse(cr, uid, tea_worker_ids):
            if tea_worker.is_selected:
                total_tea_workers += 1                
        rubber_worker_ids = self.pool.get('bpl.selected.rubber.workers.line').search(cr, uid, [('rubber_line_worker_id', '=', this.id)])
        for rubber_worker in self.pool.get('bpl.selected.rubber.workers.line').browse(cr, uid, rubber_worker_ids):
            if rubber_worker.is_selected:
                total_rubber_workers += 1                
        sundry_worker_ids = self.pool.get('bpl.selected.sundry.workers.line').search(cr, uid, [('sundry_line_worker_id', '=', this.id)])
        for sundry_worker in self.pool.get('bpl.selected.sundry.workers.line').browse(cr, uid, sundry_worker_ids):
            if sundry_worker.is_selected:
                total_sundry_workers += 1                
        other_worker_ids = self.pool.get('bpl.selected.other.workers.line').search(cr, uid, [('other_line_worker_id', '=', this.id)])
        for other_worker in self.pool.get('bpl.selected.other.workers.line').browse(cr, uid, other_worker_ids):
            if other_worker.is_selected:
                total_other_workers += 1
    total_workers = total_tea_workers + total_rubber_workers + total_sundry_workers + total_other_workers                                                                             
    res[this.id] = total_workers 
    return res
  

我将整数字段更改为功能字段

'total_workers':fields.function(_compute_workers,type =“integer”,string =“Total Workers”),

2 个答案:

答案 0 :(得分:3)

除了创建的记录的ID之外,您不应该在create()中返回任何其他内容。通常这是通过确实通过super(myclass, self).create(...)将调用结果返回到父级来完成的。

您要使用total_workers代码尝试实现的目标尚不清楚。如果total_workers应该是计算字段,则根本不必覆盖create:只需将此列声明为fields.function并将代码放入相应的计算中功能

例如:

def _compute_workers(self, cr, uid, ids, fieldname, arg, context=None):
    result = dict.fromkeys(ids, False) # default result for each id
    for this in self.browse(cr, uid, ids, context=context):
        # let's assume you have a one2many called worker_ids
        result[this.id] = len(this.worker_ids)
    return result

_columns = {
    'total_workers': fields.function(_compute_workers, type="integer",
                                     string="Total Workers")
}

此时,最好好好了解OpenERP technical mementoOpenERP framework documentation,以便了解OpenERP API以及如何使用它: - )

一旦你很好地理解了模型的结构,各种字段类型,CRUD方法和继承模式,你应该能够快速浏览官方模块的源代码并找到你想要的任何例子。完成。这应该会让你的生活更轻松!

答案 1 :(得分:1)

我认为你是以非常低效的方式对工人进行统计。 您正在使用SQL过滤器来获取与ID匹配的每个worker,然后测试其中的单个标志。

我认为在select语句中添加另一个标准会更有效率。

total_tea_workers = self.pool.get('bpl.selected.tea.workers.line').search(cr, uid, [('tea_line_worker_id', '=', id), ('is_selected', '=', True)]), count=True)