10👍
You need to use TruncDay()
instead of TruncDate
. In the usage example just below that section of the documentation you’ll see the difference between the two.
TruncDate
does not take a timezone option – it uses the current timezone and gives you the date in that timezone.
I think the distinction between the two is that TruncDate
returns a DateField
which by definition cannot be timezone-aware. TruncDay
on the other hand returns a DateTimeField
(with time portion set to 00:00:00), which can be timezone aware.
4👍
From django 3.2, TruncDate now accepts tzinfo parameter. https://docs.djangoproject.com/en/dev/ref/models/database-functions/#django.db.models.functions.TruncDate
- [Django]-How to get a foreignkey object to display full object in django rest framework
- [Django]-Ckeditor_uploader Dynamic Image Upload Path
- [Django]-Where to delete model image?
- [Django]-Django: Store Hierarchical Data
- [Django]-Caught TypeError while rendering: 'BoundField' object is not iterable
0👍
After Inspecting TruncDate Class looks like it doesn’t have timezone option
class TruncDate(TruncBase):
kind = 'date'
lookup_name = 'date'
@cached_property
def output_field(self):
return DateField()
def as_sql(self, compiler, connection):
# Cast to date rather than truncate to date.
lhs, lhs_params = compiler.compile(self.lhs)
tzname = timezone.get_current_timezone_name() if settings.USE_TZ else None
sql, tz_params = connection.ops.datetime_cast_date_sql(lhs, tzname)
lhs_params.extend(tz_params)
return sql, lhs_params
- [Django]-Switch to Django Custom User Model, Groups and Permissions
- [Django]-DJANGO allow access to a certain view only from a VPN Network
- [Django]-Named JSON array in Django REST Framework