[Django]-How to solve "type object 'datetime.datetime' has no attribute 'timedelta'" when creating a new date?

9👍

You’ve imported the wrong thing; you’ve done from datetime import datetime so that datetime now refers to the class, not the containing module.

Either do:

import datetime
...article.created_on + datetime.timedelta(...)

or

from datetime import datetime, timedelta
...article.created_on + timedelta(...)

-1👍

You should use the correct import:

from datetime import timedelta

new_date = article.created_on + timedelta(0, elapsed_time_in_seconds)
👤Dos

-1👍

I got the same issue, i solved it by replacing
from datetime import datetime as my_datetime
with
import datetime as my_datetime

👤8oris

Leave a comment