在Google App Engine中搜索模型关系? [英] Searching through Model relationships in Google App Engine?

查看:96
本文介绍了在Google App Engine中搜索模型关系?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个模型:

$ p $ class Parent(db.Model):
data = db.StringProperty( )


class Child1(db.Model):
parent = db.ReferenceProperty(Parent)
childData = db.StringProperty()

class Child2(db.Model):
parent = db.ReferenceProperty(Parent)
childData2 = db.StringProperty()
$ b $ class Child3(db.Model):
parent = db.ReferenceProperty(Parent)
childData3 = db.StringProperty()

....



我想要一个查询,它可以给我一个所有没有孩子的父母的列表。我怎么做?



我不想在父模型中为每个孩子维护一个标识符,因为我想经常添加新的子模型。

解决方案

处理这个问题的正确方法是在您的Parent实体中保留一定数量的子项。然后,您可以简单地查询孩子数量等于0的父母。



您不需要在父实体中拥有子标识;你想保留一个的儿童人数。孩子是一种型号还是另一种型号并不一定重要。保持这种运行计数的目的是让你解决你的问题,这是查询无子女的父母。数据存储区或提供此功能的AppEngine查询对象没有本地功能。



因此,由于本地不支持,所以您可以选择执行以下操作之一有两种方法:

1)保持孩子数量的计数,每当你添加或移除孩子时增加或减少;
$ 2)查询每个类型的每一个孩子并记录父母的唯一值。

2b)查询所有父实体并将它们与唯一的父亲进行匹配来自2a的ID

我会让你决定哪种方法是正确的。


I have 2 models:

class Parent (db.Model) :
  data = db.StringProperty()


class Child1 (db.Model) :
  parent = db.ReferenceProperty(Parent)
  childData = db.StringProperty()

class Child2 (db.Model) :
  parent = db.ReferenceProperty(Parent)
  childData2 = db.StringProperty()

class Child3 (db.Model) :
  parent = db.ReferenceProperty(Parent)
  childData3 = db.StringProperty()

....

I want a query which can give me a list of all parents which do not have a child yet. How do i do it?

I do not want to maintain an identifier in the Parent model for each of the children as I want to add new child models very often.

解决方案

The correct way to handle this is to keep a count in your Parent entity of the number of children. Then, you can simply query for Parents that have child count equal to 0.

You don't need to have the child id in the Parent entity; you want to keep a count of the number of children. It is not necessarily important whether the child is of one Model type or another. The purpose of keeping this running count is that it enables you to solve your problem, which is querying for childless Parents. There is no native capability of the datastore or of AppEngine query objects that provides this functionality.

So since this is not supported natively, you have the choice of doing it one of two ways:

1) keep a running count of the number of children, incrementing or decrementing whenever you add or remove a child;

2a) query EVERY child of every type and record unique values of parent.

2b) query all Parent entities and match them up with the unique parent ids from 2a

I'll leave it to you to decide which approach is the correct one.

这篇关于在Google App Engine中搜索模型关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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