使用Django DateQuerySet
我從Group
查詢中抽取item
對象的相關年份。在Django中按月份/年份註釋(組)日期
>>> Group.objects.all().dates('item__date', 'year')
[datetime.date(1990, 1, 1), datetime.date(1991, 1, 1), ...(remaining elements truncated)...']
現在我想通過不同年份對這些日期進行計數。我認爲這將工作:
>>> Group.objects.all().dates('item__date', 'year').annotate(Count('year'))
FieldError: Cannot resolve keyword 'year' into field.
但看起來像我失去了一些東西。我該如何解決這個問題?
我也試過這個查詢:
>>> (Group
.objects
.all()
.extra(select=
{'year':
connections[Group.objects.db].ops.date_trunc_sql('year', 'app_item.date')}))
ProgrammingError: missing FROM-clause entry for table "app_item" LINE 1: SELECT (DATE_TRUNC('year', app_item.date)) AS...
但是,這並不工作。
可能重複[Django:按日期(日,月,年)](http://stackoverflow.com/questions/8746014/django-group-by-date-day-month-year) – tback