【问题标题】:Identify entity key in ndb based on a property, then delete the entity根据属性识别ndb中的实体键,然后删除实体
【发布时间】:2014-07-01 22:16:20
【问题描述】:

我正在尝试基于电子邮件地址识别用户帐户(帐户实体)的 ndb 查询,然后删除该实体。帐户实体具有 access_lvl 的父级。

以下代码似乎没有正确获取帐户密钥和/或错误地使用 delete() 语句。

def access_lvl_key(website_sect_name=DEFAULT_WEBSITE_NAME):
    """Constructs a Datastore key for an Access_lvl entity with website_sect_name."""
    return ndb.Key('Access_lvl', website_sect_name)


class Account(ndb.Model):
    """Models an individual access_lvl entry."""
    email = ndb.StringProperty(required=True)
    nickname = ndb.StringProperty(indexed=True)
    date = ndb.DateTimeProperty(auto_now_add=True)
    author = ndb.UserProperty()

    @classmethod
    def find_account(self, _email, _website_sect_name):
        website_key = ndb.Key('Acess_lvl', _website_sect_name)
        account = Account.query(Account.email == _email, ancestor=website_key)
        return account.get()



class Deluser(webapp2.RequestHandler):
    def post(self):
        _website_sect_name = self.request.get('website_sect_name', DEFAULT_WEBSITE_NAME)
        _email = self.request.get('email')

        account = Account.find_account(_email, _website_sect_name)
        ndb.Key('Account', account).delete()

最终,我想为 GAE 网站的某些部分创建一个基于帐户的访问列表,该列表将经过 Google 身份验证的帐户与 ndb 帐户列表进行匹配。如果有人碰巧有任何链接,我也会很感激。

【问题讨论】:

  • The following code doesn't seem to be picking up the Account key properly and/or is using the delete() statement incorrectly. 你能说得清楚点吗?它到底(不)做什么?

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


【解决方案1】:

Access 拼写错误:

website_key = ndb.Key('Acess_lvl', _website_sect_name)

要删除具有祖先的 ndb 实体,语法是

ndb.Key('Ancestor_name', ancestor_key.id(), Entity, entity.key.id()).delete()

在你的情况下,

ndb.Key('Access_lvl', access_lvl_key(_website_sect_name).id(), Account, account.key.id()).delete()

【讨论】:

    【解决方案2】:

    Access 拼写错误:

    website_key = ndb.Key('Acess_lvl', _website_sect_name)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-10
      • 2015-10-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多