【问题标题】:Appengine object has no attribute '_projection'Appengine 对象没有属性“_projection”
【发布时间】:2018-04-01 18:45:30
【问题描述】:

我用这个

class Ad(db.Model): #change to ndb.model 
    ndb_usr = ndb.KeyProperty()

但是当我尝试这个if ad.ndb_usr: 时,我得到了错误。为什么?

【问题讨论】:

    标签: app-engine-ndb google-app-engine-python


    【解决方案1】:

    您似乎忘记实际应用评论中提到的更改:#change to ndb.model

    因此,您尝试在 db.Model 对象中引用 ndb.KeyProperty() 属性(很可能特定于 ndb.Model 对象)。由于这两个类非常相似,但并不完全相同,当在另一个类的实例上调用时,一个类中的某些代码很可能不会立即失败并取得相当大的进展(在可能难以预测的路径上)。

    我尝试使用现有代码进行回购(仅将模型从 ndb.Model 更改为 db.Model),我遇到了类似的错误,但不完全相同(嗯,不同的代码):

    AttributeError: type object 'ApartCILabel' has no attribute 'query'
    

    AttributeError: type object 'ApartCILabel' has no attribute '_get_kind'
    

    例如,最后一个很容易解释 - ndb.Model 有一个 _get_kind 方法,db.Model 没有。来自NDB Cheat Sheet

    class MyModel(db.Model):             class MyModel(ndb.Model):
    
    @classmethod                         @classmethod
    def kind(cls):                       def _get_kind(cls): 
        return 'Foo'                         return 'Foo'
    

    更新:

    我看到 DB to NDB Client Library Migration 迁移指南已经出现,很可能是比上述(旧)备忘单更好的参考。

    【讨论】:

    • 19 小时后,我已经将整个应用程序重写为 NDB,它好多了。一切正常。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-01
    • 2021-08-03
    • 1970-01-01
    相关资源
    最近更新 更多