python - 异常发生后,字段重置或清理

标签 python python-2.7 odoo odoo-8

创建异常后,我想重置该字段,因为我尝试了很多替代方案,但它没有发生,但它毫无值(value)......例如:

@api.onchange('time_date')  
def onchange_date_time(self):

        print 'working till now ----------------------------'
        if self.time_date:
            time_date = datetime.datetime.strptime(self.time_date, "%Y-%m-%d").date()
            if time_date<datetime.date.today():
                raise except_orm('Error','Please enter the correct date')
                self.time_date = None
            else:
                pass 

上面的代码,当输入日期不是酒店模块中的今天日期时,我使用它来创建异常,这里 time_date 指的是入住日期,但是它创建了一个异常,但日期字段没有重置。

最佳答案

如果您使用onchange(),则可以使用警告而不是异常。

试试这个:-

    @api.onchange('time_date')
def _onchange_date_time(self):
    if self.time_date:
        time_date = datetime.datetime.strptime(self.time_date, "%Y-%m-%d").date()
        if time_date<datetime.date.today():
            self.time_date = False
            return {
                'warning': {
                    'title': "Error",
                    'message': "Please enter the correct date",
                }
            } 
        else:
            pass 

希望这有帮助。

关于python - 异常发生后,字段重置或清理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33492595/

相关文章:

python - 为什么我仍然得到 'DeferredAttribute' object has no attribute 'objects' ?

python - Pandas groupby 找出正确和错误的百分比

python-2.7 - IO错误 : [Errno 13] Permission denied: '/usr/local/lib/netscape/mime.types'

python - 如何使用嵌套列表作为输入?

xml - 如何在行(行)而不是列中组织多对多复选框?

python - 收到错误 "Wrong python code defined for salary rule"odoo 11

Python - dateutil.parser 中的模糊

python - logstash:试图弄清IntelMQ在ElasticSearch中传递的字符串

python - 亚马逊 SES SMTP Python 使用

python - "relation already exists"在 odoo 中添加 Many2many 字段后