繁体   English   中英

在NDB GAE数据存储区的子实体中获取ID

[英]Get the ID in a child entity of NDB GAE Datastore

我正在圈中获取 NDB数据存储区的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而不是key_name保存实体,那么如何在实体内部找到正确的ID

这是我正在尝试的混合代码。

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)

文档状态:

The identifier may be either a key "name" string assigned by the application or an integer numeric ID generated automatically by the Datastore.

由于您是在Contact类上定义name属性,因此将其用作标识符。 (您不希望这样,因为在现实世界中,不同的用户可以使用相同的名称)

因此,如果您希望NDB为您的实体生成数字ID,请将name属性重命名为其他name ,例如username

更新:让我们逐步进行:

第一个示例的问题是您试图在Query上获取id 查询类没有定义id属性。 您应该在其上调用get()

# get() or fetch() should be called on query to return some data
contacts = Contact.query(Contact.email==email,ancestor=user_key).get()
self.response.write(contacts.id) # there is no attribute such as Contact.id

第二段代码的问题在于,您只是初始化一个Key并将电子邮件作为id提供-构造函数的第二个参数是id并且您将电子邮件作为值提供。 因此,您将收到电子邮件。 另外,这里没有数据库操作。

注意:标识符( idkeyurlsafevalue (用于查询))应由webapp2.RequestHandler从HTTP请求通过解析的url或HTTP POST,GET,PUT或DELETE传递。

如果您没有从HTTP请求传递的任何标识符 ,则可能很难访问特定实体 (或记录)。 因此,重要的是要注意传递一种标识符或值的形式来访问特定实体 (或数据库术语的记录)。

因此,您可以执行以下操作获取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)
    contact = contacts.get()
    id = contact.key.id() # this access the entity id directly because you have the data.
    self.response.write(id)

通过urlsafe访问:

def get(self,urlString):
  user = users.get_current_user()
  if user:
    contact_key = ndb.Key(urlsafe=urlString) #urlString refers to the key of contact
    contact = contact_key.get()
    id = contact.key.id() # this access the entity id directly because you have the data.
    self.response.write(id)

通过HTTP POST请求访问:

def post(self):
  user = users.get_current_user()
  if user:
    user_key = ndb.Key('UserPrefs',user.email())
    email = self.request.get('email')
    contacts = Contact.query(Contact.email==email,ancestor=user_key)
    contact = contacts.get()
    id = contact.key.id() # this access the entity id directly because you have the data.
    self.response.write(id)

暂无
暂无

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

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