Search code examples
pythonodooodoo-15

AttributeError: 'int' object has no attribute 'days' [Odoo 15]


I'm getting this error when I try to delete a reservation. I can't understand why, please help <3

Here is my code

class HotelReservationLine(models.Model):
    
    checkin = fields.Datetime(string="Checkin", related='line_id.checkin')
    checkout = fields.Datetime(string="Checkout", related='line_id.checkout')
    days = fields.Integer(string="Days", compute='_calculate_days')
    
    @api.depends('checkin', 'checkout')
    def _calculate_days(self):
        for rec in self:
            date = rec.checkout - rec.checkin
            rec.days = date.days

This is the traceback

RPC_ERROR Odoo Server Error Traceback (most recent call last): File "/opt/odoo/custom/src/odoo/odoo/addons/base/models/ir_http.py", line 237, in _dispatch result = request.dispatch() File "/opt/odoo/custom/src/odoo/odoo/http.py", line 687, in dispatch result = self._call_function(**self.params) File "/opt/odoo/custom/src/odoo/odoo/http.py", line 359, in _call_function return checked_call(self.db, *args, **kwargs) File "/opt/odoo/custom/src/odoo/odoo/service/model.py", line 94, in wrapper return f(dbname, *args, **kwargs) File "/opt/odoo/custom/src/odoo/odoo/http.py", line 348, in checked_call result = self.endpoint(*a, **kw) File "/opt/odoo/custom/src/odoo/odoo/http.py", line 916, in call return self.method(*args, **kw) File "/opt/odoo/custom/src/odoo/odoo/http.py", line 535, in response_wrap response = f(*args, **kw) File "/opt/odoo/auto/addons/web/controllers/main.py", line 1343, in call_kw return self._call_kw(model, method, args, kwargs) File "/opt/odoo/auto/addons/web/controllers/main.py", line 1335, in _call_kw return call_kw(request.env[model], method, args, kwargs) File "/opt/odoo/custom/src/odoo/odoo/api.py", line 464, in call_kw result = _call_kw_multi(method, model, args, kwargs) File "/opt/odoo/custom/src/odoo/odoo/api.py", line 451, in _call_kw_multi result = method(recs, *args, **kwargs) File "/opt/odoo/auto/addons/hotel_reservation/models/hotel_reservation.py", line 211, in unlink return super(HotelReservation, self).unlink() File "/opt/odoo/auto/addons/mail/models/mail_thread.py", line 338, in unlink res = super(MailThread, self).unlink() File "/opt/odoo/custom/src/odoo/odoo/models.py", line 3700, in unlink self.flush() File "/opt/odoo/custom/src/odoo/odoo/models.py", line 5644, in flush self.recompute() File "/opt/odoo/custom/src/odoo/odoo/models.py", line 6117, in recompute process(field) File "/opt/odoo/custom/src/odoo/odoo/models.py", line 6101, in process field.recompute(recs) File "/opt/odoo/custom/src/odoo/odoo/fields.py", line 1243, in recompute self.compute_value(recs) File "/opt/odoo/custom/src/odoo/odoo/fields.py", line 1265, in compute_value records._compute_field_value(self) File "/opt/odoo/custom/src/odoo/odoo/models.py", line 4255, in _compute_field_value getattr(self, field.compute)() File "/opt/odoo/auto/addons/hotel_reservation/models/hotel_reservation.py", line 592, in _calculate_days rec.days = date.days Exception

The above exception was the direct cause of the following exception:

Traceback (most recent call last): File "/opt/odoo/custom/src/odoo/odoo/http.py", line 643, in _handle_exception return super(JsonRequest, self)._handle_exception(exception) File "/opt/odoo/custom/src/odoo/odoo/http.py", line 301, in _handle_exception raise exception.with_traceback(None) from new_cause AttributeError: 'int' object has no attribute 'days'


Solution

  • When the value of checkin and checkout is not set the rec.checkout - rec.checkin expression is evaluated to 0 (False - False) and when Odoo tries to get days from date, it fails because date in this case is an integer.

    It seems like Odoo triggered the compute method after it sets the checkin and checkout field values to False

    To fix that, compute the difference in days only if the two dates are set

    Example:

    @api.depends('checkin', 'checkout')
    def _calculate_days(self):
        for rec in self:
            if rec.checkin and rec.checkout:
                date = rec.checkout - rec.checkin
                rec.days = date.days
            else:
                rec.days = 0