我一直试图为AppEngine上部署的应用程序实现数据模型。
以下是该模型的一个示例:
EntityName
id: 1
author: 'me'
description: 'whatever'
datetime: 2011 etc.
events: [
{location: [1,2], phase: ['A', 'B']},
{location: [3,4], phase: ['C', 'B']},
... more events ...
]
说明:
EntityName
有一些属性,还有很多events
event
都有一个数字列表(其location
)和一个字符串列表(phase
) 如何在AppEngine中实施此模型?只需id
,author
,datetime
和{{1}可搜索数据}。
我试图弄清楚该怎么做(仍然通过文档)(用斜体字来表示):
description
,EntityName
作为ListProperty
events
和EntityName
答案 0 :(得分:2)
这些不是表格,但我认为这应该适合你
class EntityName(db.Model):
author = db.StringProperty(...)
class Event(db.Model):
entity_name = db.ReferenceProperty(EntityName, collection_name='events')
location = db.StringListProperty()
phase = db.StringListProperty()
则...
e = EntityName(author='me', ...)
e.put()
event = Event(entity_name=e, location=...)
event.put()
for ev in e.events:
print ev.location
print ev.phase
See the documentation for details。您不需要“进行连接”,因为这不是SQL数据库。为方便起见,collection_name将在一对多关系的引用方创建一个迭代器。
答案 1 :(得分:0)
这是一种只使用一种模式的替代方案(即尼克的想法):
import webapp2
from google.appengine.ext import db
class EntityName(db.Model):
author = db.StringProperty()
# ...
event_locations = db.StringListProperty(indexed=False)
event_phases = db.StringListProperty(indexed=False)
class MainHandler(webapp2.RequestHandler):
def get(self):
e = EntityName(author='me',
event_locations=['1,2', '3,4'], event_phases=['A,B', 'C,D'] )
e.put()
q = EntityName.all().filter('author =', 'me')
text = ''
for en in q:
for locations, phases in zip(en.event_locations, en.event_phases):
text += "location=%s, phase=%s<br>" % (locations, phases)
self.response.out.write(text)
app = webapp2.WSGIApplication([('/', MainHandler)], debug=True)
示例输出:
location=1,2, phase=A,B
location=3,4, phase=C,D
由于List和StringList属性中的order is generally preserved,您的事件的位置和阶段可以匹配 - 即,它们在两个列表中具有相同的索引。
使用一个模型,你需要做更多的工作来分割列表中的每个条目,但是你节省了数据存储区的读写,因为你只有一个实体。