GAE - 从键删除实体时出错 [英] GAE - Trouble Deleting Entity from key

查看:97
本文介绍了GAE - 从键删除实体时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法使用python / ndb从GAE中的键中删除实体。我能够从页面检索实体(问题)键,但无法弄清楚如何让它删除。 GAE支持页面表示,它像检索密钥然后删除它一样简单,如下所示。

  problem = problem_key.get()
problem.key.delete()

但这不起作用。我很确定关键是正确获得。 HTML看起来像

  {%为问题中的问题%} 
< tr>
< td> {{problem.tags}}< / td>
< td>< script type =math / tex> {{problem.content}}< / script>< / td>
< td> {{problem.answer}}< / td>
< td> {{problem.quiz}}< / td>
< td> {{problem.id}}< / td>
< td> {{problem.date}}< / td>
< form action =/ deleteProblemmethod =POST>
< td>< button type =submitname =problem_keyvalue ={{problem.key}}>删除问题< / td>< / td>
< / form>
< / tr>
{%endfor%}

我的python代码如下所示:

  class deleteHandler(BaseHandler):
def post(self):
prob_key = self.request.get('problem_key' )
problem = prob_key.get()
problem.key.delete()
self.redirect(/)

我得到:

  problem = prob_key.get() 
AttributeError:'unicode'对象没有属性'get'

我最好的猜测是prob_key被分配了实际的unicode,所以当然没有 get()方法,但我不明白如何解决这个问题的基础上谷歌的解释如何它应该起作用。

编辑:好的,一直在寻找一段时间,我改变了这一行:

  prob_key = self.request.get('problem_key')



<到>

  prob_key = ndb.Key('Problem',int(self.request.get('problem_id')

因为据我了解,一个键可以按照以下方式构建:

  ndb.Key('kind','id')

不幸的是,我仍然无法让它工作。我现在正在获取

  problem.key.delete()
AttributeError:NoneType没有属性'key'


解决方案

您无法通过HTML页面传递关键对象本身,但是你可以传递它的 .urlsafe()字符串表示。

所以在你的HTML中显示字符串表示,像这样:

  value ={{problem.key.urlsafe()}}

$ b $ p


$ b $ p $在您的处理程序代码中恢复其字符串表示形式的键: p> prob_key = ndb.Key(urlsafe = self.request.get('problem_key'))

从关键字中检索实体


您也可以使用实体的密钥来获得一个适合
的编码字符串嵌入到URL中:

  url_string = sandy_key.urlsafe()

这产生一个结果,例如 agVoZWxsb3IPCxIHQWNjb3VudBiZiwIM ,其中
可以稍后用于重建键并检索原始
实体:

$ p $ sandy_key = ndb.Key(urlsafe = url_string)
sandy = sandy_key.get ()



I am having trouble deleting entities from keys in the GAE using python/ndb. I am able to retrieve the entity (problem) key from the page, but cannot figure out how to get it to delete. The GAE support page says it's as simple as retrieving the key and then deleting it, as seen below.

problem = problem_key.get()
problem.key.delete()

But this is not working. I'm pretty sure the key is being obtained correctly. The HTML looks like

{%for problem in problems %}
    <tr>
        <td>{{ problem.tags }}</td>
        <td><script type="math/tex">{{ problem.content }}</script></td>
        <td>{{ problem.answer }}</td>
        <td>{{ problem.quiz }}</td>
        <td>{{ problem.id }}</td>
        <td>{{ problem.date }}</td>
        <form action="/deleteProblem" method="POST">
            <td><button type="submit" name="problem_key" value="{{ problem.key }}">Delete Problem</button></td> 
        </form>
    </tr>
{% endfor %}    

And my python code looks like:

class deleteHandler(BaseHandler):
    def post(self):
        prob_key = self.request.get('problem_key')
        problem = prob_key.get()
        problem.key.delete()
        self.redirect("/")

I'm getting:

problem = prob_key.get()     
AttributeError: 'unicode' object has no attribute 'get'

My best guess is that prob_key is getting assigned the actual unicode and so of course there is no get() method for it but I don't understand how to fix the problem based on Google's explanation of how it should function.

EDIT: Okay, been scouting around for a while and I've changed this line:

prob_key = self.request.get('problem_key')

to

prob_key = ndb.Key('Problem', int(self.request.get('problem_id')

because as far as I understand it, a key can be constructed in the following manner

ndb.Key('kind', 'id')

Unfortunately, I still cannot get it to work. I am now getting

   problem.key.delete()
AttributeError: NoneType has no attribute 'key'

解决方案

You can't pass the key object itself through the HTML page, but you can pass its .urlsafe() string representation.

So in your HTML display the string representation, like this:

value="{{ problem.key.urlsafe() }}"

And in your handler code restore the key from its string representation:

prob_key = ndb.Key(urlsafe=self.request.get('problem_key'))

From Retrieving Entities from Keys:

You can also use an entity's key to obtain an encoded string suitable for embedding in a URL:

url_string = sandy_key.urlsafe()

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()

这篇关于GAE - 从键删除实体时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆