I want to query to MySQL using Django.models, but since I put my created_at
(Datetime field) with datetime.datetime.now()
, it seems to hard to "group by" with each date. I made a query like this:
start = time.strptime("2012/6/25","%Y/%m/%d")
end = time.strptime("2012/6/26","%Y/%m/%d")
unix_start= time.mktime(start)
unix_end = time.mktime(end)
Mymodels.objects.filter(created_at__range[datetime.datetime.fromtimestamp(unix_start),datetime.datetime.fromtimestamp(unix_end]).values("~~~~~").annotate(player_id=Coun
t("player_id"))
but I guess this query is too heavy for network.Is there any way to "group by" by datetime.date or use (date() for MySQL)in Django.models when my MySQL data is datetime.datetime.now()?
I use PostgreSQL date_trunc()
to group values by a date when the dates are saved as Datetime. I think you can use something like this with MySQL:
select_date = {"date": """DATE_FORMAT(start, '%Y-%m-%d')"""}
query = query.extra(select=select_date).values('date')
query = query.annotate(player_id=Count('player_id', distinct=True)).order_by('date')