您可以使用自定义聚合函数来生成查询:
WEEK_FUNC = 'STRFTIME("%%%%W", %s)' # use 'WEEK(%s)' for mysqlclass WeekCountAggregate(models.sql.aggregates.Aggregate): is_ordinal = True sql_function = 'WEEK' # unused sql_template = "COUNT(%s)" % (WEEK_FUNC.replace('%%', '%%%%') % '%(field)s')class WeekCount(models.aggregates.Aggregate): name = 'Week' def add_to_query(self, query, alias, col, source, is_summary): query.aggregates[alias] = WeekCountAggregate(col, source=source, is_summary=is_summary, **self.extra)>>> game_objects.extra(select={'week': WEEK_FUNC % '"games_game"."date"'}).values('week').annotate(count=WeekCount('pk'))
但是由于此API未公开,并且已经需要一些原始SQL,因此使用原始查询可能会更好。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)