can t compare offset-naive and offset-aware datetimes

0 votes

While I'm Trying to Compare Two dates I got Below Error
**can't compare offset-naive and offset-aware datetimes**

*Here Is my Check availability function*
```
import datetime
from booking.models import Booking
from mainapp.models import Room
def check_avaliblity(room,Check_in,Check_out):
    avaliblity_list=[]  #It will return binch of True and False
    booking_list=Booking.objects.filter(room=room)#It will check bookings of specific room ex:101
    for booking in booking_list:
        if booking.Check_in>Check_out or booking.Check_out<Check_in:
            #booking.check_in and booking.check_out is existing booking
            avaliblity_list.append(True)
        else:
            avaliblity_list.append(False) #If any of list in avaliblity_list is get False The Booking cannot happend
    return all(avaliblity_list)
```
This is My Booking Model
```
class Booking(models.Model):
    """Django data model Booking"""
    user=models.ForeignKey(settings.AUTH_USER_MODEL,on_delete=models.CASCADE)

    room=models.ForeignKey(Room,on_delete=models.CASCADE)
    Check_in = models.DateTimeField(datetime.datetime.strptime(str(datetime.datetime.now()),'%Y-%m-%d %H:%M:%S.%f').strftime("%Y-%m-%d %H:%M"))
    Check_out = models.DateTimeField(datetime.datetime.strptime(str(datetime.datetime.now()),'%Y-%m-%d %H:%M:%S.%f').strftime("%Y-%m-%d %H:%M"))
 
    class Meta:
        verbose_name = 'Booking'
        verbose_name_plural = 'Bookings'

    def __str__(self):
        return f'{self.user} booked {self.room} from {self.Check_in} to {self.Check_out}'

```
And Finally
Views.py

```
if request.method=='POST':
    #This is from booking page
        get_roomType=Room_Type.objects.get(roomtype=request.POST['type'])
        roomid=get_roomType.id
        getout=request.POST['check_out']  
        check_out=datetime.datetime.strptime(getout,'%m/%d/%Y %H:%M %p').strftime('%Y-%m-%d %H:%M:%S+00:00')
        getin=request.POST['check_in']  
        check_in=datetime.datetime.strptime(getin,'%m/%d/%Y %H:%M %p').strftime('%Y-%m-%d %H:%M:%S+00:00')
        check_in=datetime.datetime.strptime(check_in,'%Y-%m-%d %H:%M:%S+00:00')
        check_out=(datetime.datetime.strptime(check_out,'%Y-%m-%d %H:%M:%S+00:00'))
        print(type(check_out))
    #This can set the values id to roomtype id
        room_list=Room.objects.filter(room_type_id=get_roomType)
        user_book=request.user
        avalible_rooms=[]
        for room in room_list:
            if avalablity.check_avaliblity(room,check_in,check_out):
                avalible_rooms.append(room)
                if len(avalible_rooms)>0:
                    room=avalible_rooms
                    book_room=Booking.objects.create(
                        user=user_book,
                        room=room,
                        Check_in=check_in,
                        Check_out=check_out
                    )
                    book_room.save()
                    return HttpResponse(book_room)
            else:
                return HttpResponse("Not found")
    type_of_room=Room_Type.objects.get(roomtype=room)
    context={'type':type_of_room}
    return render(request,'app/book.html',context)
```

This is Error


Here is my Explaination
I'm Waiting for Your Reply Guys


Dec 12, 2020 in Python by Mohamed
• 170 points
1,786 views

Hi, @Mohamed,

You can have a look here?

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.

Related Questions In Python

0 votes
1 answer

Python error "TypeError: Cannot compare types 'ndarray(dtype=int64)' and 'str'"

Hey @Ashish, change the emotion_map to the ...READ MORE

answered May 30, 2019 in Python by Mir
8,080 views
–1 vote
2 answers
0 votes
1 answer

compare two csv files and print the unique string in 3rd csv file.

Hi, @Smilish, I hope this below code will ...READ MORE

answered Jun 1, 2020 in Python by Gitika
• 65,890 points
7,183 views
+1 vote
4 answers

How can I concatenate str and int objects?

If you want to concatenate int or ...READ MORE

answered Oct 18, 2018 in Python by subhm
1,245 views
0 votes
1 answer

How can I compare the content of two files in Python?

Assuming that your file unique.txt just contains ...READ MORE

answered Apr 16, 2018 in Python by charlie_brown
• 7,720 points
2,603 views
0 votes
1 answer

How can I find out the index of an element from row and column in Python?

You probably want to use np.ravel_multi_index: [code] import numpy ...READ MORE

answered Apr 16, 2018 in Python by charlie_brown
• 7,720 points
2,238 views
0 votes
1 answer

How to temporarily disable a foreign key constraint in MySQL?

Hello @kartik, To turn off foreign key constraint ...READ MORE

answered Jun 23, 2020 in Python by Niroj
• 82,880 points
2,334 views
0 votes
1 answer

How do I use Django templates without the rest of Django?

Hello @kartik, Let's say you have this important ...READ MORE

answered Jun 23, 2020 in Python by Niroj
• 82,880 points
1,485 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP