2017-04-13 154 views
0

如何在检查之前的记录是否处于“打开”状态后,针对“开放”状态的患者创建新的检查记录。如果存在,则显示错误,否则创建新记录。如何根据id和odoo中的状态来检查记录存在

下面的代码没有正确地检查'if record.id:'中的记录存在,因此直接引发错误消息。

这里是我的代码:

@api.multi 
@api.constrains('status', 'patient_id') 
def _checkstatus(self): 
    res = [] 

    for record in self: 
     if record.id: 
      if record.status == 'open': 
       raise ValidationError(_('You can not create new visit until patient checked out !')) 
      else: 
       res = self.status  
     else: 
       res = self.status 

    return res    

回答

1

你必须先提取所有记录与患者和状态==开放。然后 这样,

self.env['your.model.name'].search([('patient_id', '=', patient_id), ('status', '=', 'open')]) 

如果有符合条件的记录提出一个错误。 这里写一个代码为ex。根据您的代码更改名称。

@api.multi 
@api.constrains('status', 'patient_id') 
def _checkstatus(self): 
    res = [] 

    modelObj = self.env['your.model.name'] 
    for record in self: 
     rec = modelObj.search([('patient_id', '=', record.patient_id), ('status', '=', 'open')]) 
     if rec: 
      raise ValidationError(_('You can not create new visit until patient checked out !')) 
     else: 
      // write a login if there is not found any open record with patient 

    return res 
0

在Odoo,评估约束的时候,记录已经被创建/更新。

这意味着,在你的情况下,你需要排除当前记录集从你的查找数据(你想知道其他病人的条目是否处于“打开”状态)。

此处产生错误会导致Odoo以其交易数据为rollback,并阻止任何持续修改。

@api.multi 
@api.constrains('status', 'patient_id') 
def _checkstatus(self): 
    # retrieve patients in the current records 
    patients_tocheck = self.mapped('patient_id') 
    duplicates == self.search([('patient_id', 'in', patients_tocheck.ids), ('id', 'not in', self.ids), ('status', '=', 'open')]) 
    if duplicates: 
     raise ValidationError(_('You can not create new visit until patient checked out !')) 
    # moreover, you don't need to return any value, Odoo only see if you raise any error, or not 
+0

它为我的案件工作,非常感谢你 – majid

+0

不客气^^你可以只将你的问题设置为回答? 谢谢你^^ –

相关问题