Django---Hotel-Management-System
Django---Hotel-Management-System copied to clipboard
/guests error
NameError at /guests/
name 'Booking' is not defined
Request Method: | GET |
---|---|
http://127.0.0.1:8000/guests/ | |
3.1.4 | |
NameError | |
name 'Booking' is not defined | |
/code/github/Django---Hotel-Management-System/HMS/accounts/models.py, line 15, in numOfBooking | |
code/github/venv/bin/python | |
3.8.10 | |
['/code/github/Django---Hotel-Management-System/HMS', '/usr/lib/python38.zip', '/usr/lib/python3.8', '/usr/lib/python3.8/lib-dynload', '/code/github/venv/lib/python3.8/site-packages'] | |
Thu, 19 Aug 2021 09:44:20 +0000 |
I have fixed this issue, It is showing "Booking is not defined" because the Booking model is defined inside room-->models.py and the Booking model methods are written inside account-->models.py. So you have to do is copying those methods and just put it below the Booking model inside room-->models.py. follow these steps:
Go to accounts directory, open models.py. There you can see model named "Guest", there you have to cut these lines which is selected.
Then you have to go to room directory and open models.py. There you will see a Booking model class. You have to paste those code here as shown in this figure below
or paste this
def numOfBooking(self):
return Booking.objects.filter(guest=self).count()
def numOfDays(self):
totalDay = 0
bookings = Booking.objects.filter(guest=self)
for b in bookings:
day = b.endDate - b.startDate
totalDay += int(day.days)
return totalDay
def numOfLastBookingDays(self):
try:
return int((Booking.objects.filter(guest=self).last().endDate - Booking.objects.filter(
guest=self).last().startDate).days)
except:
return 0
def currentRoom(self):
booking = Booking.objects.filter(guest=self).last()
return booking.roomNumber
Now it's fixed.