【问题标题】:how to create a function to raise error in openerp如何创建一个函数以在 openerp 中引发错误
【发布时间】:2015-07-24 11:57:34
【问题描述】:

我想创建一个在用户输入无效日期时引发错误的函数,所以如果我们输入的日期早于当前日期,它会显示错误,有什么想法吗?

我的班级看起来像:

class business_trip(orm.Model):
_columns = {'start_trip': fields.datetime('Trip Starts on:',translate=True, required=False),
        'start_business': fields.datetime('Business Starts on:',translate=True, required=False),
        'end_business': fields.datetime('Business Ends on:',translate=True, required=False),
        'end_trip': fields.datetime('Trip Ends on:',translate=True, required=False),

【问题讨论】:

    标签: openerp odoo openerp-7 raiseerror


    【解决方案1】:

    您可以添加添加 Python 约束,例如,

    @api.one
    @api.constrains('start_trip', 'start_business', 'end_business', 'end_trip')
    def _check_date(self):
        now = datetime.now().strftime(DEFAULT_SERVER_DATETIME_FORMAT)
        if self.start_trip and  now < self.start_trip:
            raise exceptions.ValidationError("Entered Date Should be greter then Today")
        elif self.start_business and now < self.start_business:
            raise exceptions.ValidationError("Entered Date Should be greter then Today")
        elif self.end_business and now < self.end_business:
            raise exceptions.ValidationError("Entered Date Should be greter then Today")
        elif self.end_trip and now < self.end_trip:
            raise exceptions.ValidationError("Entered Date Should be greter then Today")
    

    【讨论】:

      【解决方案2】:

      谢谢你们,这是我的解决方案:

        def _check_date(self,cr,uid,ids,context=None):
      
              record=self.browse(cr,uid,ids,context=None)
              for data in record:
                  if data.start_trip >= data.start_business or data.start_trip >= data.end_business or data.start_trip >= data.end_trip or data.start_business >= data.end_business or data.start_business >= data.end_trip or data.end_business >= data.end_trip:
                      return False
                  else:
                      return True
      
      _constraints = [(_check_date, 'Error: You Entered Wrong Dates, Please Enter the Right Dates ',['start_trip', 'start_business', 'end_business', 'end_trip'])]
      

      【讨论】:

        猜你喜欢
        • 2014-05-10
        • 2018-09-01
        • 1970-01-01
        • 2020-12-27
        • 2010-10-02
        • 2019-03-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多