Google应用引擎ReferenceProperty关系 [英] Google app engine ReferenceProperty relationships

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

问题描述

我正在尝试使用ReferenceProperty获取我的模型,但没有大量的运气。我有3个级别:组,主题,然后优点和缺点。正如在一个小组里面有很多话题,每一个话题可能有很多的优点和缺点。



我能够存储新的组好,但我不有任何想法如何存储这些组下面的主题。我想从一个页面链接到每个组下面的链接新主题,这将使它们变成一个简单的形式(现在有1个字段)。显然,该网址需要对该组或某些ID的某种引用。



以下是我的模型:

 类组(db.Model):

group_user = db.UserProperty()
group_name = db.StringProperty(multiline = True )
group_date = db.DateTimeProperty(auto_now_add = True)


类主题(db.Model):

topic_user = db.UserProperty()
topic_name = db.StringProperty(multiline = True)
topic_date = db.DateTimeProperty(auto_now_add = True)
topic_group = db.ReferenceProperty(Groups,collection_name ='topics')

class Pro(db.Model):

pro_user = db.UserProperty()
pro_content = db.StringProperty(multiline = True)
pro_date = db.IntegerProperty (default = 0)
pro_topic = db.ReferenceProperty(Topics,collection_name ='pros')

class Con(db.Model):

con_user = db .UserProperty()
con_content = d b.StringProperty(multiline = True)
con_date = db.IntegerProperty(default = 0)
con_topic = db.ReferenceProperty(Topics,collection_name ='cons')

另一个功能为实际页面,我想显示组列表,然后在他们的主题下:

 类摘要(webapp.RequestHandler):
def get(self):

groups_query = Groups.all()
groups = groups_query.fetch(1000)
template_values = {

'groups':groups,
}

path = os.path。 join(os.path.dirname(__ file__),'summary.html')
self.response.out.write(template.render(path,template_values))
/ pre>

最后html:

 < html> 
< body>
< a href =/ newgroup>新组< / a>
< br>
{组中的组%}

< font size =24> {{group.group_name | escape}}< / font>< br>由< b> {{group.group_user}}< / b>在< b> {{group.group_date}}< / b> {{group.raw_id}}
< br>
< a href =/ newtopic?id = {{group.key.id}}>新的topice< / a>
< br>
< blockquote>
{{topics.topics_name}}
< / blockquote>


{%endfor%}
< / body>
< / html>


解决方案

有副作用的东西,如更改商店(例如通过创建一个新对象)应该不是一个HTTP GET - GET本质上只应该执行读取操作。这不是pedantry,它是一个关键的HTTP语义 - 浏览器,缓存,代理等,被允许作为只读操作(例如通过缓存结果,而不是传递请求到服务器,如果他们对于修改,使用HTTP动词,如POST(最流行的原因是所有浏览器都正确实现)或专门的操作PUT(创建新对象)或DELETE(删除对象)。我假设您将使用POST来支持各种浏览器。



要从浏览器获取POST,您需要使用Javascript wizardy或简单的旧表单与方法= post - 我会假设后者为了简单。



如果你使用Django 1.0(哪个应用程序引擎现在支持),它有自己的机制根据模型制作,验证和接受表格。其他框架有自己的类似高级层。



如果你想避免丰富的框架,你必须用手工模板来实现你的HTML表单,通过某种URL调度,例如在app.yaml中)到使用 def post(self)实现的处理程序:,从请求获取数据,验证它,形成新的对象,放置,显示一些确认页。



程序的哪些部分或部分不清楚?您的问题的标题专注于参考属性,但我不知道他们特别给您什么问题 - 从您的问题的文字中您似乎正确地谈论他们。



修改:OP现在已经在评论中澄清了他的问题是如何做出如下一些:

 < a href =/ newtopic?id = {{group.key.id}}>新主题< / a> 

工作。有多种方法可以做到这一点。如果新版本的URL由静态表单提供,则该表单的操作的处理程序可以通过返回到 id = 引用者:标题(臭名昭着但不能混淆的拼写错误),但这有点笨重和脆弱。更好的是让一个处理程序提供新的主题URI,其中 def get 从请求中获取 id = 并将其插入在生成的表单模板中,例如,在隐藏的输入字段中。表单的模板包含(其他字段):

 < INPUT TYPE = hidden NAME = thegroupid VALUE = {{theid }}> < / INPUT> 

theid 渲染该模板,并且将在请求中接收表单的操作的 def post 获得。


I'm trying to get my models related using ReferenceProperty, but not have a huge amount of luck. I have 3 levels: Group, Topic, then Pros, and Cons. As in a Group houses many topics, and within each topic could be many Pros and Cons.

I am able to store new Groups nice and fine, but I don't have any idea how to store topics underneath these groups. I want to link from a page with a link "New topic" underneath each group, that takes them to a simple form (1 field for now). Obviously the URL will need to have some sort of reference to the id of the group or something.

Here are my models:

class Groups(db.Model):

    group_user = db.UserProperty()
    group_name = db.StringProperty(multiline=True)
    group_date = db.DateTimeProperty(auto_now_add=True)


class Topics(db.Model):

    topic_user = db.UserProperty()
    topic_name = db.StringProperty(multiline=True)
    topic_date = db.DateTimeProperty(auto_now_add=True)
    topic_group = db.ReferenceProperty(Groups, collection_name='topics')

class Pro(db.Model):

    pro_user = db.UserProperty()
    pro_content = db.StringProperty(multiline=True)
    pro_date = db.IntegerProperty(default=0)
    pro_topic = db.ReferenceProperty(Topics, collection_name='pros')    

class Con(db.Model):

    con_user = db.UserProperty()
    con_content = db.StringProperty(multiline=True)
    con_date = db.IntegerProperty(default=0)
    con_topic = db.ReferenceProperty(Topics, collection_name='cons')    

And one function for the actual page I want to show the list of Groups, and then underneath their topics:

class Summary(webapp.RequestHandler):
    def get(self):

        groups_query = Groups.all()
        groups = groups_query.fetch(1000)
        template_values = {

            'groups': groups,           
        }

        path = os.path.join(os.path.dirname(__file__), 'summary.html')
        self.response.out.write(template.render(path, template_values))

And finally the html:

<html>
  <body>
    <a href="/newgroup">New Group</a>
    <br>
    {% for group in groups %}

    <font size="24">{{ group.group_name|escape }}</font><br> by <b>{{ group.group_user }}</b> at <b>{{ group.group_date }}</b> {{ group.raw_id }}
    <br>
    <a href="/newtopic?id={{group.key.id}}" >New topice </a>
    <br>
    <blockquote>
        {{ topics.topics_name }}
    </blockquote>


    {% endfor %}
  </body>
</html>

解决方案

Something that has side effects, such as altering the store (by creating a new object for example) should NOT be an HTTP GET -- GET should essentially only do "read" operations. This isn't pedantry, it's a key bit of HTTP semantics -- browsers, caches, proxies, etc, are allowed to act on GET as read-only operations (for example by caching results and not passing a request to the server if they can satisfy it from cache).

For modifications, use HTTP verbs such as POST (most popular essentially because all browsers implement it correctly) or for specialized operations PUT (to create new objects) or DELETE (to remove objects). I assume you'll be going to use POST to support a variety of browsers.

To get a POST from a browser, you need either Javascript wizardy or a plain old form with method=post -- I'll assume the latter for simplicity.

If you're using Django 1.0 (which app engine supports now), it has its own mechanisms to make, validate and accept forms based on models. Other frameworks have their own similarly advanced layers.

If you want to avoid "rich" frameworks you'll have to implement by hand templates for your HTML forms, direct them (via some kind of URL dispatching, e.g. in app.yaml) to a handler of yours implementing with a def post(self):, get the data from the request, validate it, form the new object, put it, display some acknowledgment page.

What part or parts of the procedure are unclear to you? Your question's title focuses specifically on reference properties but I'm not sure what problem they are giving you in particular -- from the text of your question you appear to be on the right tack about them.

Edit: the OP has now clarified in a comment that his problem is how to make something like:

"<a href="/newtopic?id={{group.key.id}}" >New topic </a>" 

work. There's more than one way to do that. If the newtopic URL is served by a static form, the handler for the post "action" of that form could get back to that id= via the Referer: header (a notorious but unfixable mis-spelling), but that's a bit clunky and fragile. Better is to have the newtopic URI served by a handler whose def get gets the id= from the request and inserts it in the resulting form template -- for example, in a hidden input field. Have that form's template contain (among the other fields):

<INPUT TYPE=hidden NAME=thegroupid VALUE={{ theid }}> </INPUT>

put theid in the context with which you render that template, and it will be in the request that the def post of the action receiving the form finally gets.

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

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