Spaceships Spaceships - 2 years ago 176
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?

Answer Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download