# list only objects in choosen year

**URL:** <https://forum.djangoproject.com/t/list-only-objects-in-choosen-year/5061>\
**Category:** Using Django\
**Created:** [November 3, 2020, 2:50pm UTC](https://forum.djangoproject.com/t/list-only-objects-in-choosen-year/5061 "2020-11-03T14:50:08Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ad-software](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/ad-software/32/2350_2.png) [@ad-software](https://forum.djangoproject.com/u/ad-software)\
**Post date:** [November 3, 2020, 2:50pm UTC](https://forum.djangoproject.com/t/list-only-objects-in-choosen-year/5061/1 "2020-11-03T14:50:08Z")

</div>

I have a list of years with events.  
Now I want to list only events of a choosen year.

views.py

```
def get_years_with_events(request):
queryset = Event.objects.all().order_by('-date')
years = []
for item in queryset:
    year = item.date.year
    if not year in years:
        years.append(year)
context = {
    "year_list": years
}
return render(request, "events/year_list.html", context)

def events_list_view(request):
queryset = Event.objects.all().order_by('-date')
context = {
    "object_list": queryset
}
return render(request, "events/events_list.html", context)

{% extends 'base.html' %}

```

{% block content %}

year\_list.html  
{% include ‘events/events\_navbar.html’ %}

Liste Jahre mit Veranstaltungen

  
{% if year\_list %}  
{% for year in year\_list %}  

{{ year }}

{% endfor %}  
{% else %}

Es sind noch keine Veranstaltungen erfasst.

  
{% endif %}  
{% endblock %}

How can I filter for a year? Somehow I need to pass the year from year\_list.html to the events\_list\_view as a filter or to get\_years\_with\_events to filter the queryset and return year\_list and queryset.

Can you please help me?

---

<div class="post-metadata">

**Author:** ![typonaut](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/typonaut/32/2316_2.png) [@typonaut](https://forum.djangoproject.com/u/typonaut)\
**Post date:** [November 3, 2020, 4:34pm UTC](https://forum.djangoproject.com/t/list-only-objects-in-choosen-year/5061/2 "2020-11-03T16:34:54Z")

</div>

I don’t think I can give you the exact code, but with the assumption all events have a date:

Get the value of the ‘year’ you want as a string (eg 2020), cast that string as a datetime type (`startdate = datetime.strptime('2020', '%Y'`), add one year to `stratdate` (`enddate = startdate + timedelta(years=1)`).

You don’t need any more date information than the year, in this case `print(startdate)` outputs `2020-01-01 00:00:00`.

Then create a filter where your event date is `>= startdate` and `< enddate`.

Hope that isn’t too abstract for you.

---

<div class="post-metadata">

**Author:** ![CodenameTim](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/codenametim/32/31507_2.png) [@CodenameTim](https://forum.djangoproject.com/u/CodenameTim)\
**Post date:** [November 3, 2020, 5:02pm UTC](https://forum.djangoproject.com/t/list-only-objects-in-choosen-year/5061/3 "2020-11-03T17:02:49Z")

</div>

You could annotate your queryset of Events with the [extracted year](https://docs.djangoproject.com/en/3.1/ref/models/database-functions/#extract). The issue here is timezones. If you store your datetimes in UTC, then convert them to the requesting user’s timezone, then you may run into issues around timestamps near the new year.

```
from django.db.models.functions import ExtractYear

tzinfo = None # Or however else you want to set the timezone. If you don't, it'll just use the timezone that the date was stored in.

# Extract the year off the date and put it into a new field on event.
all_events = Event.objects.annotate(
    year=ExtractYear('date', tzinfo=tzinfo)
)

# Fetch a list of all distinct year values for the events.
# This is a good candidate for caching.
years = list(all_events.values_list('year', flat=True).distinct())

# Filter down to the events in the selected year or show all events.
filtered_events = all_events.filter(year=selected_year) if selected_year else all_events
```

---

<div class="post-metadata">

**Author:** ![ad-software](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/ad-software/32/2350_2.png) [@ad-software](https://forum.djangoproject.com/u/ad-software)\
**Post date:** [November 4, 2020, 8:53pm UTC](https://forum.djangoproject.com/t/list-only-objects-in-choosen-year/5061/4 "2020-11-04T20:53:27Z")

</div>

Thank you for the help. I solved it.  
`<a class="dropdown-item" href="{% url 'events-for-year-list' year %}">{{ year }}</a>`

```
`path('<int:year>/', events_for_year_list_view, name='events-for-year-list'),`
```
