Christopher H
Christopher H

Reputation: 2194

Django list of dates

In Django I have a SQL table with a column with a list of dates. The field is DATE.

In the views.py file I would like to get a list of the years from those dates. I have tried the following with no luck:

from mysite.WeatherData.models import WeatherData

time_list =[]
raw_time_list = WeatherData.objects.all()

for onedatum in raw_time_list:
    time_list += onedatum.time_stamp.isocalendar()[0]

the column in WeatherData is called time_stamp and it holds the Date data.

The error I get is:
'int' object is not itterable.

I have done this with weeks of the year with WeatherData.objects.filter(location = LocationCode) and it worked fine so I'm not sure why this doesn't work now.

Upvotes: 1

Views: 533

Answers (1)

mechanical_meat
mechanical_meat

Reputation: 169334

You get that error because an integer cannot be appended to a list.
Here is an example to reproduce the error:

>>> l = []
>>> l += 1
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'int' object is not iterable
>>> l.append(1) # but we can use the .append() method instead
>>> l
[1]

Applied to your code:

for onedatum in raw_time_list:
    time_list.append(onedatum.time_stamp.isocalendar()[0])

Upvotes: 3

Related Questions