Skip to Content
Menu
This question has been flagged
1 Reply
3205 Views

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?

Avatar
Discard
Best Answer

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

Avatar
Discard
Author

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

Author

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

Related Posts Replies Views Activity
0
Dec 19
3773
2
Jan 16
34010
1
Aug 20
3522
1
Apr 20
4069
1
Sep 19
7707