718π
The problem is not in Django settings, but in the date passed to the model. Hereβs how a timezone-aware object looks like:
>>> from django.utils import timezone
>>> import pytz
>>> timezone.now()
datetime.datetime(2013, 11, 20, 20, 8, 7, 127325, tzinfo=pytz.UTC)
And hereβs a naive object:
>>> from datetime import datetime
>>> datetime.now()
datetime.datetime(2013, 11, 20, 20, 9, 26, 423063)
So if you are passing email date anywhere (and it eventually gets to some model), just use Djangoβs now()
. If not, then itβs probably an issue with an existing package that fetches date without timezone and you can patch the package, ignore the warning or set USE_TZ to False.
209π
Use django.utils.timezone.make_aware function to make your naive datetime objects timezone aware and avoid those warnings.
It converts naive datetime object (without timezone info) to the one that has timezone info (using timezone specified in your django settings if you donβt specify it explicitly as a second argument):
import datetime
from django.conf import settings
from django.utils.timezone import make_aware
naive_datetime = datetime.datetime.now()
naive_datetime.tzinfo # None
settings.TIME_ZONE # 'UTC'
aware_datetime = make_aware(naive_datetime)
aware_datetime.tzinfo # <UTC>
- [Django]-What's the difference between select_related and prefetch_related in Django ORM?
- [Django]-Serving Media files during deployment in django 1.8
- [Django]-How to stop autopep8 not installed messages in Code
64π
Just to fix the error to set current time
from django.utils import timezone
import datetime
datetime.datetime.now(tz=timezone.utc) # you can use this value
- [Django]-Removing 'Sites' from Django admin page
- [Django]-FileUploadParser doesn't get the file name
- [Django]-Django datefield filter by weekday/weekend
- [Django]-How to understand lazy function in Django utils functional module
- [Django]-Django error: got multiple values for keyword argument
- [Django]-Why am I getting this error in Django?
24π
make sure settings.py has
USE_TZ = True
In your python file:
from django.utils import timezone
timezone.now() # use its value in model field
- [Django]-'staticfiles' is not a valid tag library: Template library staticfiles not found
- [Django]-Create custom buttons in admin change_form in Django
- [Django]-What is related_name used for?
17π
One can both fix the warning and use the timezone specified in settings.py, which might be different from UTC.
For example in my settings.py I have:
USE_TZ = True
TIME_ZONE = 'Europe/Paris'
Here is a solution; the advantage is that str(mydate)
gives the correct time:
>>> from datetime import datetime
>>> from django.utils.timezone import get_current_timezone
>>> mydate = datetime.now(tz=get_current_timezone())
>>> mydate
datetime.datetime(2019, 3, 10, 11, 16, 9, 184106,
tzinfo=<DstTzInfo 'Europe/Paris' CET+1:00:00 STD>)
>>> str(mydate)
'2019-03-10 11:16:09.184106+01:00'
Another equivalent method is using make_aware
, see dmrz post.
- [Django]-The QuerySet value for an exact lookup must be limited to one result using slicing. Filter error
- [Django]-Difference between User.objects.create_user() vs User.objects.create() vs User().save() in django
- [Django]-Authenticate by IP address in Django
14π
If you are trying to transform a naive datetime into a datetime with timezone in django, here is my solution:
>>> import datetime
>>> from django.utils import timezone
>>> t1 = datetime.datetime.strptime("2019-07-16 22:24:00", "%Y-%m-%d %H:%M:%S")
>>> t1
datetime.datetime(2019, 7, 16, 22, 24)
>>> current_tz = timezone.get_current_timezone()
>>> t2 = current_tz.localize(t1)
>>> t2
datetime.datetime(2019, 7, 16, 22, 24, tzinfo=<DstTzInfo 'Asia/Shanghai' CST+8:00:00 STD>)
>>>
t1 is a naive datetime and t2 is a datetime with timezone in djangoβs settings.
- [Django]-Django's Double Underscore
- [Django]-Django Installed Apps Location
- [Django]-Django Generic Views using decorator login_required
13π
You can also override settings, particularly useful in tests:
from django.test import override_settings
with override_settings(USE_TZ=False):
# Insert your code that causes the warning here
pass
This will prevent you from seeing the warning, at the same time anything in your code that requires a timezone aware datetime may give you problems. If this is the case, see kravietz answer.
- [Django]-Django DRF with oAuth2 using DOT (django-oauth-toolkit)
- [Django]-Change a form value before validation in Django form
- [Django]-Images from ImageField in Django don't load in template
8π
In the model, do not pass the value:
timezone.now()
Rather, remove the parenthesis, and pass:
timezone.now
If you continue to get a runtime error warning, consider changing the model field from DateTimeField to DateField.
- [Django]-Django Rest Framework custom response message
- [Django]-Exclude fields in Django admin for users other than superuser
- [Django]-Disabled field is not passed through β workaround needed
1π
I use function to covert date
-> datetime
aware format like this:
from datetime import datetime, date
from django.utils import timezone
def date_to_datetime_aware(d:date) -> datetime:
dt = datetime.combine(d, datetime.min.time())
dt_aware = timezone.make_aware(dt)
return dt_aware
It helps me to awoid Django RuntimeWarning.
- [Django]-Django switching, for a block of code, switch the language so translations are done in one language
- [Django]-How do I use django rest framework to send a file in response?
- [Django]-Gunicorn Connection in Use: ('0.0.0.0', 5000)
0π
If you need to convert the actual date string to date object, I have got rid of the warning by simply using astimezone
:
>>> from datetime import datetime, timezone
>>> datetime_str = '2013-09-04 14:14:13.698105'
>>> datetime_object = datetime.strptime(datetime_str, "%Y-%m-%d %H:%M:%S.%f")
>>> datetime_object.astimezone(timezone.utc)
datetime.datetime(2013, 9, 4, 6, 14, 13, 698105, tzinfo=datetime.timezone.utc)
- [Django]-Parsing unicode input using python json.loads
- [Django]-Why am I getting this error in Django?
- [Django]-Django apps aren't loaded yet when using asgi
-1π
I encountered this warning when using the following model.
from datetime import datetime
class MyObject(models.Model):
my_date = models.DateTimeField(default=datetime.now)
To fix it, I switched to the following default.
from django.utils import timezone
class MyObject(models.Model):
my_date = models.DateTimeField(default=timezone.now)
- [Django]-Error: could not determine PostgreSQL version from '10.3' β Django on Heroku
- [Django]-How to print BASE_DIR from settings.py from django app in terminal?
- [Django]-How to specify an IP address with Django test client?