【问题标题】:Google App Engine: A handler to delete 'any' entityGoogle App Engine:删除“任何”实体的处理程序
【发布时间】:2014-03-08 07:32:11
【问题描述】:

目前可以使用路径 /12345 查看我的实体,其中 12345 代表其 entity ID

我想创建一个处理程序,它使用/12345/delete 删除任何实体。

处理程序看起来像这样:

class DeleteHandler(BaseHandler):
    def get(self, entity_id):
       # cannot retrieve entity using just entity_id
       # We cannot use KIND.get_by_id(entity_id), since 'entity kind' is unknown

如何通过url获取实体种类,以便使用get_by_id()检索实体,然后删除?

我的处理方法正确吗?还是有更好的办法?

【问题讨论】:

  • 那么/12345/delete 任何id 为12345 的实体?
  • 是的,先生 - 这是正确的@JimmyKane
  • 感觉不对

标签: python google-app-engine app-engine-ndb webapp2


【解决方案1】:

实体 id 在各种类型中并不是唯一的,所以我真的不知道您期望它如何工作。

但是,完整密钥的网络安全编码版本确实提供了足够的信息来唯一标识实体。然后你可以用它来实例化一个键,你可以在那个键上调用 delete。

【讨论】:

    【解决方案2】:

    感觉不对。

    我会选择kind/12345/delete

    要回答您的问题,一种方法是使用白名单。

    在某处设置您希望此操作删除其对象(实体)的kinds(类)。

    假设你有这些种类:

    class Foo(ndb.Expando):
      pass
    
    
    class Boo(ndb.Expando):
      pass
    
    
    class Bar(ndb.Expando):
      pass
    

    现在既然你有了 id:

    kinds_to_delete = ['Foo', 'Boo', 'Bar']
    for kind_to_delete in kinds_to_delete:
      ndb.Key(kind_to_delete, entity_id).delete()
    

    或者作为一个肮脏的单线:

    [ndb.Key(kind, entity_id).delete() for kind in ['Foo', 'Boo', 'Bar']]

    【讨论】:

      【解决方案3】:
      /<key>/delete 
      

      如果你传递 key 的 base64 表示可能会更容易

      【讨论】:

        猜你喜欢
        • 2012-11-06
        • 1970-01-01
        • 2015-07-25
        • 1970-01-01
        • 1970-01-01
        • 2010-09-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多