【发布时间】:2013-10-06 00:54:52
【问题描述】:
我正在获取 NDB Datastore 的 id 上兜圈子。
我已设置webapp2.RequestHandler 来接收电子邮件并获取 ID。基本上我的目标是删除一个实体,但是如果我通过电子邮件地址来获取实体的 ID,我就很难过,因为它给了我刚刚得到的结果。我用 ID 代替了key_name。
我尝试通过电子邮件查询查找 ID,但似乎使用查询没有方法属性来查找 ID。
def get(self,email):
user = users.get_current_user()
if user:
user_key = ndb.Key('UserPrefs',user.email())
contacts = Contact.query(Contact.email==email,ancestor=user_key)
self.response.write(contacts.id) # there is no attribute such as Contact.id
我试图通过获取密钥来查找 ID,但是当我显示密钥时,它显示了我在 email 变量中的任何值
def get(self,email):
user = users.get_current_user()
if user:
user_key = ndb.Key('UserPrefs',user.email())
contact_key = ndb.Key('Contact',email,parent=user_key)
self.response.write(contact_key.id())
真正的问题:那么,鉴于我没有 ID,如果我在实体中找到正确的 ID通过 id 而不是 key_name 保存了我的实体?
这是我正在尝试的混合代码。
def get(self,email):
user = users.get_current_user()
if user:
user_key = ndb.Key('UserPrefs',user.email())
contact_key = ndb.Key('Contact',email,parent=user_key)
contacts = Contact.query(Contact.email==email,ancestor=user_key)
contact = contacts.get()
contact_key.delete()
# self.response.write(contact.name) # this works
self.response.write(contact_key.id()) # this does not work because I do not have the entity id, and I'd like to get it blindfolded. Is there a way?
这是我的联系模型。
class Contact(ndb.Model):
name = ndb.StringProperty()
phone = ndb.StringProperty()
email = ndb.StringProperty()
dateCreated = ndb.DateTimeProperty(auto_now_add=True)
dateUpdated = ndb.DateTimeProperty(auto_now=True)
【问题讨论】:
标签: google-app-engine key app-engine-ndb