Recursive delete in google app engine

前端 未结 4 1143
长发绾君心
长发绾君心 2021-02-09 17:37

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 thi

4条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-02-09 18:31

    Reconsider the data structure. If the relationship will never change on the record lifetime, you could use "ancestors" feature of GAE:

    class Top(db.Model): pass
    class Middle(db.Model): pass
    class Bottom(db.Model): pass
    
    top = Top()
    middles = [Middle(parent=top) for i in range(0,10)]
    bottoms = [Bottom(parent=middle) for i in range(0,10) for middle in middles]
    

    Then querying for ancestor=top will find all the records from all levels. So it will be easy to delete them.

    descendants = list(db.Query().ancestor(top))
    # should return [top] + middles + bottoms
    

提交回复
热议问题