2017-07-12 49 views
0

我想在字典中使用new_variable = current_user.pub使用從數據庫酒館列中的數據,但我得到這個錯誤AttributeError: 'NoneType' object has no attribute 'pub'對此的任何幫助深表感謝在Python字典中使用current_user變量?

這裏是我的全部代碼`

pins = { 
     4 : {'name' : 'Channel 1', 'board' : 'esp8266', 'topic' : '/4', 'state' : 'False'}, 
     5 : {'name' : 'Channel 2', 'board' : 'esp8266', 'topic' : '/5', 'state' : 'False'} 
     } 

    variable = pins[4]['topic'] 
    new_variable = current_user.pub 
    pins[4]['topic'] = new_variable 

謝謝你

+1

你在哪裏定義'current_user'? – Dadep

+0

因爲'current_user'沒有定義,所以'NoneType'沒有'pub'屬性,創建'current_user'來使用它 – depperm

+0

嘿謝謝回覆 實際上'''''''''''列 –

回答

0

使用python @property裝飾器在你的類中創建一個方法。無論你想要什麼,都可以回到酒吧。

class User(db.Models): 
    id = db.Column(db.Integer, primary_key=True) 
    key = db.Column(db.String(80)) 

    @property 
    def pub(self): 
     return "{}".format(self.key) 
+0

謝謝你的答案試過這個,但我得到了這個錯誤 'TypeError:'property'類型的對象沒有len()' –

+0

你正在做什麼?顯然'pub'不會返回'list'。 –

+0

這就是我所做的 4:{'name':'Channel 1','board':'esp8266','topic':'/ 4','state':'False'} , 5:{ '名稱': '頻道2', '板': 'esp8266', '主題': '/ 5', '州': '假'} } @property DEF酒館(自): 返回 「{}」 的格式(self.key) 變量=引腳[4] [ '主題'] 酒館= 「{} current_user.pub」 銷[4] [ '主題'] =酒館' –