Spaceships Spaceships - 11 months ago 77
Python Question

Django unorderable types

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

is a date given from DRF:

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

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?


You could use timezone to get the current date time:

from django.utils import 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):

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

import datetime
now =

More Here