python - How to write Django QuerySet to get UNIX_TIMESTAMP of a DateTimeField? -
this question follow-up 1 here.
suppose have following django model:
from django.db import models django.db.models import sum class mymodel(models.model): my_string = models.charfield(max_length=32,) my_date = models.datetimefield() @staticmethod def get_stats(): logger.info(mymodel.objects.values('my_string').annotate( count=count('my_string'), sum=sum(f('my_date')+0)), #this needs change. what?? ) )
instead of calculating sum of my_date
, calculate sum of mysql expression unix_timestamp('my_date')
. how can modify django queryset above accomplish that?
note: question not duplicate of this one because asking how value queryset (ie: how database return value). in question, there no database. straight non-django python. i'm using django.
look @ func-expressions
from django.db.models import func, f, sum queryset.annotate(sum=sum(func(f('my_date'), function='unix_timestamp')))
Comments
Post a Comment