2013-04-12 99 views
0

這是我寫的一些代碼,用於給出一些子通道的高級搜索結果,這些子通道具有每個不同表中的屬性值和屬性值。它給我這個錯誤:'Value' object does not support indexing at line 35'Value'對象不支持索引

sub_id = request.GET['sub_ch_id'] 
attributes = Attribute.objects.filter(subchannel_id = sub_id) 
values =[] 
print "attributes" 
# print request 
post = [] 
value_obj =[] 
for w in attributes: 
    name = w.name 
    print request.GET[name] 
    values.append(request.GET[name]) 



result_search_obj = [] 
flag = False 
result_search = [] 
result = [] 
post = [] 
i = 0 
f = i+1 
# post_temp = "" 

# print "HIIIIII", len(result_search_obj) 
for j in range(0,len(attributes)): 
    # print 'EREEEEEEE' 
    result_search_obj+=(Value.objects.filter(attribute_id = attributes[j].id 
     , value = values[j])) 
    # print '1st loop' 
result_search = [[] for o in result_search_obj]  
for k in range(0,len(result_search_obj)): 
    # print '2 loop' 
    for l in range(0,len(result_search_obj)): 
     print 'why u dnt go here' 

     result_search[k].append(result_search_obj[k][l].Post_id) 
     # print '4 loop' 
for a in range(0,len(result_search)): 

    result_search.sort(len(result_search[k])) 
    # print '6 loop' 
for h in range(0,len(result_search)): 

    post_temp = "" 
    # print '3 loop' 
    for g in result_search[h]: 
     tmp=result_search[h] 
     loc = temp[g] 
     if loc == result_search[h+1][g]: 
      flag = True 
      post_temp = tmp[g] 
      break 
    post = post_temp 
print post 

return HttpResponse('filter_post_channel.html', {'posts' : post}) 
+0

親愛的穆罕默德·35行是在你的代碼空的,是這樣的你的完整文件?或者你能指出哪條線正在引發錯誤? – Alexis

+0

第一次循環後,向我們顯示'print result_search_obj'的輸出,請 – soon

回答

1

我認爲這個問題是這一行:

result_search_obj+=(Value.objects.filter(attribute_id = attributes[j].id 
    , value = values[j])) 

您正在創建一個元組,並將它附加到列表中。您期望將元組添加爲元組,但是Python會平滑元組並添加其元素。

所以,你需要改變你的線路,以創建一個列表,並把它添加到result_search_obj

result_search_obj+= [ (Value.objects.filter(attribute_id = attributes[j].id 
    , value = values[j])) ] 

樣品測試

>>> x=[] 
>>> x += (1, 2) 
>>> x 
[1, 2] 
>>> x += [(1, 2)] 
>>> x 
[1, 2, (1, 2)] 
>>> x += (1, 2) 
>>> x 
[1, 2, (1, 2), 1, 2] 
>>> 
+0

非常感謝您解決了這個問題:D –