Spaceships Spaceships - 7 months ago 39
Python Question

Django unorderable types

Using Django I want to check if a date is 30 days in the future,

kwargs['datetime']
is a date given from DRF:

from datetime import date, timedelta
if kwargs['datetime'] > timedelta(days=30):
pass


I get the error:
unorderable types: datetime.datetime() > datetime.timedelta()

Is a Django timezone not compatible with datatime or I'm I doing something else wrong?

AKS AKS
Answer

You could use timezone to get the current date time:

from django.utils import timezone
now = timezone.now()

and, to check if the datetime from DRF is after 30 days in future you could add timedelta to now

# or >= as per your requirement
if kwargs['datetime'] > now + timedelta(days=30):
    pass

If timezone support is disabled, you could get the current datetime as follows:

import datetime
now = datetime.datetime.now()

More Here