Can anyone explain the difference between 'django.db.models.functions.Now' and 'django.utils.timezone.now'?
For example, am I correct that functions.Now()
returns the system time without timezone stamp, and timezone.now()
would return UTC timestamp?
When I use print(timezone.now())
I get the expected timestamp however when I print(functions.Now())
I get a blank string. Why is this?
In what circumstances is it appropriate to use one method over the other?
The timezone.now(…)
function [Django-doc] returns a datetime
object with the timezone, given the USE_TZ
setting [Django-doc] is used.
Now
[Django-doc] on the other hand, is a database expression. Indeed, it does not determine a datetime
. It will use NOW()
at the database side where you query. It will thus work with the clock of the database. If the database thus runs on the same server, it is the server time, whereas if it runs on a different server, it can be a few seconds/minutes earlier/later if the clocks of the two systems are not completely in sync.
If you thus query with:
from django.utils import timezone
Post.objects.filter(published_at__lte=timezone.now())
Then it will make a query that looks like:
SELECT post.* FROM post WHERE published_at <= 2022-07-27 15:40:00 -- time from Django/Python
whereas if we use:
from django.db.models.functions import Now
Post.objects.filter(published_at__lte=Now())
it will query with:
SELECT post.* FROM post WHERE published_at <= NOW() -- databae timestamp