简体   繁体   English

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

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

I'm going in circles on getting the id of NDB Datastore. 我正在圈中获取 NDB数据存储区的ID

I have setup the webapp2.RequestHandler to catch the email and get the ID. 我已经设置了webapp2.RequestHandler来捕获电子邮件并获取ID。 Basically my goal is to delete an entity, but if I pass the email address to get the ID of the entity, I'm stump, because it gives me results I was just getting. 基本上,我的目标是删除一个实体,但是如果我通过电子邮件地址获取该实体的ID,那我很尴尬,因为它给了我刚得到的结果。 I used ID instead of key_name . 我使用ID代替key_name

I tried finding the ID by querying via email , but it seems like using query does not have a method attribute to find the id. 我尝试通过电子邮件查询来查找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

I tried to find the ID by getting the key , but when I displayed the key, it showed me whatever value I have in the email variable 我试图通过获取密钥找到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())

Real Question: So, given that I do not have the ID , how do I find the correct ID inside an entity if I saved my entities via id and not key_name? 真正的问题:因此,鉴于我没有ID ,如果我通过id而不是key_name保存实体,那么如何在实体内部找到正确的ID

Here are the mixture of codes that I'm trying out. 这是我正在尝试的混合代码。

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?

Here is my Model for Contact. 这是我的联系方式。

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 docs state: 文档状态:

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

Since you are defining the name property on your Contact class, this is used as the identifier. 由于您是在Contact类上定义name属性,因此将其用作标识符。 (You don't want that because in real world different users can have same names) (您不希望这样,因为在现实世界中,不同的用户可以使用相同的名称)

So if you want NDB to generate numeric IDs for your entities, rename the name property to something else, eg username . 因此,如果您希望NDB为您的实体生成数字ID,请将name属性重命名为其他name ,例如username

Update: let's go step by step: 更新:让我们逐步进行:

Problem with the first example is that you are trying to get id on the Query . 第一个示例的问题是您试图在Query上获取id Query class has no id property defined on it. 查询类没有定义id属性。 You should call get() on it: 您应该在其上调用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

Problem with the second piece of code is that you are just initialising a Key and providing email as id - the second param of constructor is the id and you are providing email as value. 第二段代码的问题在于,您只是初始化一个Key并将电子邮件作为id提供-构造函数的第二个参数是id并且您将电子邮件作为值提供。 Hence you are getting the email out. 因此,您将收到电子邮件。 Also, there is no database operation here. 另外,这里没有数据库操作。

Note: the identifiers, which are id , key , urlsafe , or value (for the query) should be passed from the HTTP Request by webapp2.RequestHandler from a parsed url or HTTP POST, GET, PUT, or DELETE. 注意:标识符( idkeyurlsafevalue (用于查询))应由webapp2.RequestHandler从HTTP请求通过解析的url或HTTP POST,GET,PUT或DELETE传递。

If you do not have any identifiers or values passed from an HTTP request, it could be difficult to access the specific entity (or the record). 如果您没有从HTTP请求传递的任何标识符 ,则可能很难访问特定实体 (或记录)。 So, it is important to take note to pass a form of identifier or value to access the specific entity (or the record in database terms). 因此,重要的是要注意传递一种标识符或值的形式来访问特定实体 (或数据库术语的记录)。

So, you can do the following to get the id: 因此,您可以执行以下操作获取ID:

Access by value: 按值访问:

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)

Access by urlsafe: 通过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)

Access by HTTP POST Request: 通过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