GAE DataStore引用属性关系 [英] GAE DataStore referenceProperty relationship

查看:93
本文介绍了GAE DataStore引用属性关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让所有的家长/孩子从一对多的关系中获益。传统上,我可以通过连接来实现这一点,但是在数据存储中这样做会逃避我。



我发现了几个部分示例来执行此操作,但没有一个是完成了。

我有:

  class拥有者(db。模型):
name = db.StringProperty()

class Pet(db.Model):
petname = db.StringProperty()
owner = db.ReferenceProperty (所有者,collection_name ='pets')

#现在我想打印scott拥有的所有宠物
scott = Owner(name =scott)
scott.put )
Pet(owner = scott,petname =rufus)。put()

pets = Pet.all()。filter('owner =',scott)。fetch (100)
print Pet.all()。filter('owner =',scott)。fetch(0)


解决方案

如果您删除scott周围的引号,我认为您的查询应该可以正常工作。

<您也可以让Scott的所有宠物实体将其实体作为其父母:

  cl所有者(db.Model):
name = db.StringProperty()
$ b $ class Pet(db.Model):
petname = db.StringProperty()

scott =所有者(name =scott)
scott.put()
$ b $ Pet(父= scott,petname =rufus)。put()$ b $ (宠物名=snoogums)。put()

宠物= Pet.all宠物(父母=斯科特,宠物名=蓬松)。 ().ancestor(scott).fetch(100)
#将宠物喂给你的模板引擎。可以这么说。

通过将scott作为Pet实体的父项,它们全部添加到同一个实体组中,并且 ancestor 查询为您提供了一种简单且stragihtforward的方式来获取所有 Pet 所有者'。您应该比祖先查询获得更好的性能。



这确实限制了Pet实体只能属于一个实体组,并且如果您希望有一个涉及多个数据关系的 Pet ,则必须选择另一种方法。如果它是一对一的关系,只需将引用存储到其他相关实体。



要打印 Pet 实体,给它一个 __ unicode __ 方法,如下所示:

  class Pet(db.Model):
petname = db.StringProperty()
$ b $ def __unicode __(self):
returnI am+ self .petname

__ unicode __ 应该返回一个字符串您要查看的信息由 print 语句打印。正如Nick在评论中明智地指出的那样,您不应该在AppEngine应用程序中使用 print 。 SDK带有Django模板和Jinja2。使用其中一个或导入您喜欢的。


I am trying to get all parent/children from a one to many relationship. Traditionally, I could do this with a join, but doing so in the datastore is escaping me.

I have found several partial examples to do this, but not one that is complete yet.

I have:

class Owner(db.Model):
  name = db.StringProperty()

class Pet(db.Model):
  petname = db.StringProperty()
  owner = db.ReferenceProperty(Owner, collection_name='pets')

#now I want to print all pets owned by scott
scott = Owner(name="scott")
scott.put()
Pet(owner=scott,petname="rufus").put()

pets = Pet.all().filter('owner =', "scott").fetch(100)
print Pet.all().filter('owner =', "scott").fetch(0)

解决方案

Your query should work fine if you remove the quotes around "scott", I think.

You could also make all of Scott's Pet entities have his entity as their parent:

class Owner(db.Model):
    name = db.StringProperty()

class Pet(db.Model):
    petname = db.StringProperty()

scott = Owner(name="scott")
scott.put()

Pet(parent=scott, petname="rufus").put()
Pet(parent=scott, petname="fluffy").put()
Pet(parent=scott, petname="snoogums").put()

pets = Pet.all().ancestor(scott).fetch(100)
# Feed pets to your templating engine. So to speak.

By making scott the parent of the Pet entities, they are all added to the same entity group, and the ancestor query gives you a simple and stragihtforward way to get all of the Pet entities that are children of the given `Owner'. You should get much better performance with an ancestor query than a non-ancestor query.

This does impose the limitation that the Pet entities can only belong to one entity group, and if you wanted to have a Pet involved in multiple data relationships, you would have to choose another approach. If it is a one-to-one relationship, just storing a Reference to the other related entity.

To have a printable representation of your Pet entity, give it a __unicode__ method, something like this:

class Pet(db.Model):
    petname = db.StringProperty()

    def __unicode__(self):
        return "I am " + self.petname

__unicode__ should return a string with the information that you want to see printed by the print statement. As Nick wisely points out in comments, you should not use print in an AppEngine application. The SDK comes with Django templates and Jinja2. Use one of those or import one that you prefer.

这篇关于GAE DataStore引用属性关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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