开发者

python app engine Model query for models in the same group

开发者 https://www.devze.com 2023-02-28 16:38 出处:网络
I have transactional Groups of entities of type db.Model that have a parent defined. I want to query the datastore to only return models that are in the same group. Currently I\'m querying all objects

I have transactional Groups of entities of type db.Model that have a parent defined. I want to query the datastore to only return models that are in the same group. Currently I'm querying all objects of a 'Kind' then removing those that don't have the same root entity.

Is there a cleaner way to do this - I can't see one in the sdk.

for example:

from google.appengine.ext import db

class ParentObject(db.Model):
  ....


class ChildObject(db.Model):
  ....


parent1 = ParentObject()
parent2 = ParentObject()
child1 = ChildObject(parent=parent1)
child2 = ChildObject(parent=parent2)
child3 = ChildObject(parent=parent1)

I want to run a query that returns child1 and child开发者_JAVA技巧3 because they have the same parent.


I think this is what you are looking for.

get_chlids=ChildObject.all().ancestor(parent1)

To know more about modeling in appengine. You can have a look here


class TestFamilyTree(unittest.TestCase):

def test_family_tree(self):
  parent1 = ParentObject()
  parent1.put()
  parent2 = ParentObject()
  parent2.put()
  child1 = ChildObject(parent=parent1)
  child1.put()
  child2 = ChildObject(parent=parent2)
  child2.put()
  child3 = ChildObject(parent=parent1)
  child3.put()
  query = ChildObject.all()
  query.ancestor(child1.parent())
  siblings = query.fetch(100)
  self.assertEqual(len(siblings),2) 
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号