这个错误让我发疯:
我正在尝试遍历字典的键以检查是否在年份列表中找到它们中的任何一个,以便在views.py中按年过滤记录,并且我收到此错误。我应用过滤器的实例模型(例如past_runnings
)来自链接具有不同字段的两个模型。奇怪的是,使用包含运动类型的列表执行相同的检查而没有错误。
有什么建议吗?
以下是代码:
p_runnings = runnings.filter(race_id__race_starts__lte=datetime.now(), participated=True)
my_runnings = MyRace.objects.filter(runner_id=runner)
past_runnings = list(chain(p_runnings, my_runnings)) # instance from chained models
p_types = types #type of sport
p_years = list(set(r.race_id.race_starts.year if isinstance(r, Running) else r.race_starts.year for r in past_runnings))
# no error
for k, v in p_races_dict.iteritems():
if isinstance(v, dict):
if k in p_types:
p_runnings_filtered = past_runnings.filter(race_id__type=k).filter(type=k)
p_races_dict[k]['p_races_num'] = p_runnings_type_filtered.count()
# 'list' object has no attribute 'filter' error
for k, v in p_races_dict.iteritems():
if isinstance(v, dict):
if k in p_years:
p_runnings_filtered = past_runnings.filter(race_id__race_starts__year=k)