第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

sqlalchemy 模型初始化將模型屬性變形為無(wú)類型

sqlalchemy 模型初始化將模型屬性變形為無(wú)類型

拉風(fēng)的咖菲貓 2021-10-26 18:33:54
我有一個(gè)我正在嘗試初始化的用戶模型,但我不斷收到AttributeError: 'NoneType' 對(duì)象沒(méi)有屬性 'set'經(jīng)過(guò)進(jìn)一步檢查,我了解到 sqlalchemy 將其模型轉(zhuǎn)換為包含 InstrumentedAttribute 屬性的映射器,使它們可查詢。顯然這一切都發(fā)生在對(duì)象初始化時(shí)。這只發(fā)生在用戶模型上,沒(méi)有別的。我嘗試使用 app_context 來(lái)查看是否是由于 db 不在范圍內(nèi)。但它沒(méi)有幫助。class userData(db.Model):    __tablename__ = "users"    id = db.Column(db.Integer, primary_key=True, autoincrement=True)    name = db.Column(db.String(128), nullable=False)    # TODO need to add hashing for the password    password = db.Column(db.String(10), nullable=False)    type = db.Column(db.String(5), nullable=False)    facility_id = db.Column(db.Integer, db.ForeignKey('facility.id'), nullable=True)    auth_hash = db.Column(db.String, nullable=True)    def __init__(self, *args, **kwargs) -> None:        super(userData, self).__init__(*args, **kwargs)    def __getattr__(self, item):        if item is 'facility':            facility = facilityData.query.filter(facilityData.id == self.facility_id).first()            if facility is None:               return "No Facility Found"            else:                return facility.facility_name作為參考,這個(gè)工作得很好:class barcodeData(db.Model):    __tablename__ = 'barcodes'    id = db.Column(db.Integer, primary_key=True, autoincrement=True)    barcode = db.Column(db.String(128), nullable=False)    medid = db.Column(db.String(128), nullable=False)    user = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=True)    def __init__(self, data):        '''class constructor'''        self.barcode = data.get('barcode')        self.medid = data.get('medid')        self.user = data.get('user')這是我嘗試創(chuàng)建新的 userData 對(duì)象時(shí)遇到的錯(cuò)誤。import modelsimport appwith app.app.app_context():    models.userData(name='admin2', password='test2',  facility_id=1)我什至在沒(méi)有 app_context() 的情況下嘗試了另一種方式,然后專門從模型中導(dǎo)入 userData。還嘗試在每次迭代中交替使用 app_context。
查看完整描述

1 回答

?
烙印99

TA貢獻(xiàn)1829條經(jīng)驗(yàn) 獲得超13個(gè)贊

所以才意識(shí)到為什么會(huì)發(fā)生這種行為?;旧希c db 范圍或任何此類無(wú)關(guān)。


問(wèn)題是因?yàn)樵陬惐粚?shí)例化的過(guò)程中,類的屬性變成了可查詢的屬性,這意味著 sqlalchemy 需要引用這些屬性。它是通過(guò)__getattr__在創(chuàng)建對(duì)象之前使用順序推斷類型(db.Column)來(lái)完成的。


因?yàn)?,我覆蓋了__getattr__希望使我的輸出更容易序列化到 json ,它使用默認(rèn)返回的值None。這就是導(dǎo)致錯(cuò)誤的原因。


@property

def facility(self)->str:

    # do stuff to get facility_name

    return facility_name


查看完整回答
反對(duì) 回復(fù) 2021-10-26
  • 1 回答
  • 0 關(guān)注
  • 187 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)