1

在 Django 中,我有一个 SQL 表,其中有一列包含日期列表。该字段是日期。

在views.py 文件中,我想获取这些日期的年份列表。我试过以下没有运气:

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]

WeatherData 中的列称为 time_stamp,它包含日期数据。

我得到的错误是:
'int' 对象不可迭代。

我已经用 Wea​​therData.objects.filter(location = LocationCode) 在一年中的几周内完成了这项工作,它工作得很好,所以我不确定为什么现在这不起作用。

4

1 回答 1

3

您会收到该错误,因为无法将整数附加到列表中。
这是重现错误的示例:

>>> 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]

应用于您的代码:

for onedatum in raw_time_list:
    time_list.append(onedatum.time_stamp.isocalendar()[0])
于 2012-06-11T02:24:57.627 回答