[Python] Can't compare naive and aware datetime.now() <= challenge.datetime_end

Discussão em 'Python' iniciado por Stack, Julho 4, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to compare the current date and time with dates and times specified in models using comparison operators:

    if challenge.datetime_start <= datetime.now() <= challenge.datetime_end:


    The script errors out with:

    TypeError: can't compare offset-naive and offset-aware datetimes


    The models look like this:

    class Fundraising_Challenge(models.Model):
    name = models.CharField(max_length=100)
    datetime_start = models.DateTimeField()
    datetime_end = models.DateTimeField()


    I also have django using locale date and times.

    What I haven't been able to find is the format django uses for DateTimeField(). Is it naive or aware? And how do I get datetime.now() to recognize locale datetime?

    Continue reading...

Compartilhe esta Página