如何在创建记录后立即调用我的方法。到目前为止:
def _compute_stage_deadline(self, cr, uid, ids, context=None):
for lead in self.browse(cr, uid, ids):
stage_deadline = datetime.now() + lead.stage_id.days_for_stage
print "Testing stage datetime: ", stage_deadline.strftime("%Y-%m-%d %H:%M:%S")
return stage_deadline
def create(self, cr, uid, ids, context=None):
self._compute_stage_deadline(cr, uid, ids, context)
return super(CRM_Lead, self).create(cr, uid, ids, context=context)
但是我在创建记录之前就调用它,所以在_compute_stage_deadline
中我尝试访问该记录时出现此错误(因为该记录尚不存在):
AttributeError: 'NoneType' object has no attribute 'stage_id'
我怎样才能绕过它并在创建后调用我的方法?
答案 0 :(得分:1)
使用这种方法解决了这个问题:
def _compute_stage_deadline(self, cr, uid, lead, context=None):
stage_deadline = datetime.now() + lead.stage_id.days_for_stage
print "Testing stage datetime: ", stage_deadline.strftime("%Y-%m-%d %H:%M:%S")
return stage_deadline
def create(self, cr, uid, vals, context=None):
new_id = super(CRM_Lead, self).create(cr, uid, vals, context=context)
lead = self.browse(cr, uid, new_id, context=context)
self._compute_stage_deadline(cr, uid, lead, context)
return new_id
答案 1 :(得分:0)
只使用vals而不是lead:
def create(self, cr, uid, vals, context=None):
self._compute_stage_deadline(cr, uid, vals, context)
return super(CRM_Lead, self).create(cr, uid, vals, context=context)