简体   繁体   English

在python字典中使用current_user变量?

[英]using current_user variable in python dictionary?

I am trying to use a data from pub column in database using new_variable = current_user.pub in dictionary but i am getting this error AttributeError: 'NoneType' object has no attribute 'pub' any help regarding this is much appreciated 我正在尝试使用字典中的new_variable = current_user.pub从数据库中的pub列中使用数据,但是我收到此错误AttributeError: 'NoneType' object has no attribute 'pub'对此AttributeError: 'NoneType' object has no attribute 'pub'任何帮助都非常感谢

here is my full code ` 这是我的完整代码`

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

thank you 谢谢

Use python @property decorator to create a method in your class. 使用python @property装饰器在您的类中创建一个方法。 And return the pub whatever you want. 并根据需要返回酒吧。

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM