需要获取实体键来删除实体 [英] need to get entity key to delete entity
问题描述
我试图使用html中的链接从Datastore中删除一个实体。我知道为了做到这一点,我需要拥有实体的密钥,以便我知道哪个实体将删除链接配对,可以这么说。我不能为我的生活找出如何做到这一点......
I am trying to delete an entity from the Datastore using a link in html. I understand that in order to do this, I need to have the entity's key so that I know which entity to "pair" the delete link with, so to speak. I can't for the life of me figure out how to do this...
这是我的html文件,显示数据库中的所有汽车:
Here is my html file that shows all of the cars in the database:
{% if cars|length > 0 %}
{% for c in cars %}
<tr>
<td>{{ c.make }}</td>
<td>{{ c.model }}</td>
<td>{{ c.year }}</td>
<td>
{% for i in c.color %}
{{ i }}
{% endfor %}
</td>
<td>{{ c.condition }}</td>
<td>{{ c.date }}</td>
<td>
<a href="/view_cars/{{ c.key().id() }}">Delete Car</a>
</td>
</tr>
{% endfor %}
{% endif %}
python文件:
Here is the python file:
class AddCarHandler(webapp2.RequestHandler):
template_variables = {}
def get(self):
template = JINJA_ENVIRONMENT.get_template('index.html')
self.response.write(template.render(self.template_variables))
action = self.request.get('action')
#if the user adds a car
if action == 'add_car':
c = car_database.Car()
c.make = self.request.get('car-make')
c.model = self.request.get('car-model')
c.year = self.request.get('car-year')
c.color = self.request.get_all('car-color')
c.condition = self.request.get('car-condition')
c.date = self.request.get('car-date')
car_key = c.put()
class ViewCarHandler(webapp2.RequestHandler):
template_variables = {}
def get(self):
car = car_database.Car()
#ndb query
self.template_variables['cars'] = [{'make':x.make, 'model':x.model, 'year':x.year, 'color':x.color, 'condition':x.condition, 'date':x.date} for x in car_database.Car.query().fetch()]
template = JINJA_ENVIRONMENT.get_template('/view_cars.html')
self.response.write(template.render(self.template_variables))
推荐答案
您可以像下面这样获取实体的关键字(即通过查询获得):
You can get the key of an entity (which, say, is obtained through a query) like this:
entity_key = entity.key
注意:这仅适用于 >实体保存到数据库中,而不是之前(即 entity.put()
至少被调用一次)。
Note: this only works after the entity was saved into the DB, not before (i.e. entity.put()
was called at least once).
在python代码和URL之间传递密钥或HTML代码,您可以从文档中使用密钥url字符串或基于该字符串的预先计算的删除url,例如,在 self.template_variables ['cars']
中传递:
To pass the key between the python code and URLs or HTML code, from the documentation, you can use a key's url string or a pre-computed deletion url based on that string, passed, for example, in self.template_variables['cars']
:
您也可以使用实体的密钥来获取适合
的编码字符串,以便嵌入到URL中:
You can also use an entity's key to obtain an encoded string suitable for embedding in a URL:
url_string = sandy_key.urlsafe()
结果像agVoZWxsb3IPCxIHQWNjb3VudBiZiwIM可以
以后用来重建密钥并检索原始实体:
This produces a result like agVoZWxsb3IPCxIHQWNjb3VudBiZiwIM which can later be used to reconstruct the key and retrieve the original entity:
sandy_key = ndb.Key(urlsafe=url_string)
sandy = sandy_key.get()
您需要为s添加一个处理程序你可以在上面引用这个关键字,然后调用:
You'll need to add a handler for such deletion url, in which you'd reconstruct the key as quoted above, then call:
entity_key.delete()
这篇关于需要获取实体键来删除实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!