大家好我遇到了一个问题,即在预订部分,如果房间已经预订,它应该创建一个例外,显示房间已经预订,同样的房间可以在退房日期后提前预订我用过的代码:
@api.one
@api.onchange('time_date')
def onchange_same(self):
x=self.env['hotel.management'].search([('room','=','self.room.room')])
# check_out = datetime.datetime.strptime(self.env['hotel.management'].search([('room','=','self.room.room')]).check_out, "%Y-%m-%d").date()
time_date = datetime.datetime.strptime(self.time_date, "%Y-%m-%d").date()
check_out = datetime.datetime.strptime(self.check_out, "%Y-%m-%d").date()
if x:
if time_date>=x.check_out:
pass
else:
print 'working till now--------'
raise except_orm('Payment Error!',"This room is booked please select another one")
然而它正在显示:
类型错误:必须是字符串,而不是bool
答案 0 :(得分:1)
当我们从数据库中获取任何值时,首先我们需要检查这个条件=>天气场是否具有储值。
第二件事(在你的情况下)有 search()方法的问题。
search()方法的一般语法:
self.env['model.name'].search(['field_name', 'operator', 'value'])
尝试使用此代码:
@api.one
@api.onchange('time_date')
def onchange_same(self):
x=self.env['hotel.management'].search([('room','=',self.room.room)])
if self.time_date or self.check_out:
time_date = datetime.datetime.strptime(self.time_date, "%Y-%m-%d").date()
check_out = datetime.datetime.strptime(self.check_out, "%Y-%m-%d").date()
if x and x.check_out:
if time_date>=x.check_out:
pass
else:
print 'working till now--------'
raise except_orm('Payment Error!',"This room is booked please select another one")
else:
print "\n=====date not define"
的更多信息
答案 1 :(得分:0)
感谢@Odedra指导我,但我解决了以下代码。
@api.onchange('time_date')
def onchange_same(self):
x=self.env['hotel.management'].search([('room','=',self.room.room)])
if x:
if self.time_date>=x.check_out:
pass
else:
print 'working till now--------'
raise except_orm('Payment Error!',"This room is booked please select another one")