2017-12-27 287 views
0

enter image description here類型錯誤:「賬戶」對象不是可迭代

我正在使用的Tkinter和Peewee ORM的腳本。我有:

db = SqliteDatabase('data.db') 
introspector = Introspector.from_database(db) 
models = introspector.generate_models() 
cities_list = [] 
account_obj = models['accounts'] 
recordset= account_obj.select() 
for r in recordset: 
    city = str(r.city) 
    elapsed_hours = (time.time()-int(r.datetime))/3600 
    cities_list.append(str(r.city)+'-'+str(elapsed_hours)) 


master = tk.Tk() 

variable = StringVar(master) 
variable.set(cities_list[0]) # default value 

w = OptionMenu(master, variable, *cities_list) 
w.pack() 

def ok(): 
    print ("value is:" + variable.get()) 
    v_list = variable.get().split('-') 
    print type(account_obj) 
    recordset = account_obj.select().where(account_obj.city.contains(v_list[0])).get() 
    for r in recordset: 
     r.datetime=int(time.time()) 
     r.update() 


button = Button(master, text="OK", command=ok) 
button.pack() 

mainloop() 

在第二個SELECT語句:

recordset = account_obj.select().where(account_obj.city.contains(v_list[0])).get() 

我越來越:

Traceback (most recent call last): 
<class 'peewee.BaseModel'> 
    File "...lib\lib-tk\Tkinter.py", line 1542, in __call__ 
    return self.func(*args) 
    File "... myscript.py", line 46, in ok 
    for r in recordset: 
TypeError: 'accounts' object is not iterable 

我在做什麼錯?

+1

通常,'get()'只返回一個實例(如果有的話)。你可能應該用'all()'(它返回一個'iterable *'列表)代替行:'recordset = account_obj.select()。where(account_obj.city.contains(v_list [0]) )獲得()'。希望這是有道理的,因爲我對這個框架沒有經驗。 – CristiFati

+0

recordset = account_obj.select()。where(account_obj.city.contains(v_list [0]))。all() AttributeError:'SelectQuery'對象沒有'all'屬性 – user61629

+0

我擔心會發生這種情況。然後嘗試刪除'.get()'。 – CristiFati

回答

0

在@Nae的請求,這是我工作的代碼重新排列:

master = tk.Tk() 

variable = StringVar(master) 
variable.set(cities_list[0]) # default value 

w = OptionMenu(master, variable, *cities_list) 
w.pack() 

def ok(): 
    print ("value is:" + variable.get()) 
    master.destroy() 

button = Button(master, text="OK", command=ok) 
button.pack() 

mainloop() 

v_list = variable.get().split('-') 
recordset = account_obj.select().where(account_obj.city.contains(v_list[0])) 
for r in recordset: 
    r.datetime = int(time.time()) 
    r.save() 

問題竟然是我的表,我從CSV導入的沒有一個主鍵。一旦我添加了,save()開始工作。請參閱http://docs.peewee-orm.com/en/latest/peewee/querying.html#updating-existing-records

相關問題