gpt4 book ai didi

python - 在 OpenERP ver 7 中覆盖具有多个返回的创建方法

转载 作者:太空宇宙 更新时间:2023-11-04 10:47:35 25 4
gpt4 key购买 nike

我需要在我的事件方法中做两个事件。一个是需要返回 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

已编辑

我是这样整理的,希望对大家有帮助:-)

i wrote a function for my field

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

i changed my integer field to functional field

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

最佳答案

除了创建的记录的 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 memento 是个好主意。和 OpenERP framework documentation , 以获得 OpenERP API 的概述以及它应该如何使用:-)

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

关于python - 在 OpenERP ver 7 中覆盖具有多个返回的创建方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16273491/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com