跳至內容
選單
此問題已被標幟
1 回覆
3963 瀏覽次數

I've already searching how to modify save button and get some solution like this :

def create(self, cr, uid, vals, context=None):
        # Your logic goes here or call your method
        res_id = super(reservation, self).create(cr, uid, vals, context=context)
        # Your logic goes here or call your method
        return res_id

    def write(self, cr, uid, vals, context=None):
        # Your logic goes here or call your method
        super(reservation, self).write(cr, uid, id, vals, context=context)
        # Your logic goes here or call your method
        return True

I have Hotel module and class reservation. I want user will see this message warning

if self.duration < 0:
            return {
                'warning': {
                    'title': "Something bad happened",
                    'message': "It's very bad indeed",
                    }
                }

if they break some condition like let the guest name and room number being not filled or let check out date less than check in date. Where should I locate this warning code in that function?

頭像
捨棄
最佳答案

Hi,

For this case, there is no need to override the create method or the write method. You can use the api.constrains and raise the validation error message if the user forgets to enter some value or if some value is less than the specified limit.

from odoo import _, api, fields, models
from odoo.exceptions import ValidationError


@api.constrains('duration')
def check_duration(self):
if self.duration < 0:
raise ValidationError(_("Duration Must Be Non-Zero Positive Number !"))

Thanks

頭像
捨棄
作者

Hi, should I import something? because ValidationError returns error: undefined variable

作者

Sorry to say but that don't gives any effect. Is there another solution?

This is the standard solution, rather than looking for other solutions, check why this is not working. Just search the constrains in the odoo code you will get a lot of samples

相關帖文 回覆 瀏覽次數 活動
0
12月 19
4613
2
1月 16
35776
1
8月 20
4828
1
4月 20
5369
1
9月 19
8994