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

时间:2013-10-06 00:54:52

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

我正在围绕获取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 通过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)

2 个答案:

答案 0 :(得分:2)

docs州:

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

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

因此,如果您希望NDB为您的实体生成数字ID,请将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

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

答案 1 :(得分:-1)

注意:标识符idkeyurlsafevalue(对于查询)应该从来自解析的URL或HTTP POST,GET,PUT或DELETE的webapp2.RequestHandler的HTTP请求。

如果您没有从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)