Google应用引擎中的递归删除 [英] Recursive delete in google app engine

查看:112
本文介绍了Google应用引擎中的递归删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Google应用引擎与django 1.0.2(和django-helper),并想知道人们如何去做递归删除。
假设你有一个这样的模型:

 
class Top(BaseModel):
pass

class Bottom(BaseModel):
daddy = db.ReferenceProperty(上)

现在,当我删除一个类型为Top的对象,我希望所有关联的Bottom对象也被删除。



现在,当我删除一个'Top'对象,'Bottom'对象停留,然后我获取不属于任何地方的数据。在视图中访问数据存储区时,最终会出现以下情况:

在呈现时捕获异常:ReferenceProperty无法解析。

我当然可以找到所有对象并删除它们,但是由于我的真实模型至少有5个级别,所以我希望有一种方法可以自动完成。



我发现这个文章关于它如何与Java一起工作,似乎几乎是我想要的。



任何人都知道如何在django中得到这个行为?

解决方案

您需要手动实现,通过查看受影响的记录并在删除父记录的同时删除它们。您可以通过覆盖父类上的.delete()方法来简化此操作,以自动删除所有相关记录。



出于性能原因,您几乎可以想要使用仅键查询(允许您获取要删除的实体的密钥,而不必获取和解码实际实体),并且批量删除。例如:

  db.delete(Bottom.all(keys_only = True).filter(daddy =,top) fetch(1000))


I'm using google app engine with django 1.0.2 (and the django-helper) and wonder how people go about doing recursive delete. Suppose you have a model that's something like this:

class Top(BaseModel):
    pass

class Bottom(BaseModel):
    daddy = db.ReferenceProperty(Top)

Now, when I delete an object of type 'Top', I want all the associated 'Bottom' objects to be deleted as well.

As things are now, when I delete a 'Top' object, the 'Bottom' objects stay and then I get data that doesn't belong anywhere. When accessing the datastore in a view, I end up with:

Caught an exception while rendering: ReferenceProperty failed to be resolved.

I could of course find all objects and delete them, but since my real model is at least 5 levels deep, I'm hoping there's a way to make sure this can be done automatically.

I've found this article about how it works with Java and that seems to be pretty much what I want as well.

Anyone know how I could get that behavior in django as well?

解决方案

You need to implement this manually, by looking up affected records and deleting them at the same time as you delete the parent record. You can simplify this, if you wish, by overriding the .delete() method on your parent class to automatically delete all related records.

For performance reasons, you almost certainly want to use key-only queries (allowing you to get the keys of entities to be deleted without having to fetch and decode the actual entities), and batch deletes. For example:

db.delete(Bottom.all(keys_only=True).filter("daddy =", top).fetch(1000))

这篇关于Google应用引擎中的递归删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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