我想在google app引擎中使用以下属性创建数据库
class Questions(db.Model):
title = db.StringProperty()
author = db.StringProperty()
text = db.TextProperty()
date = db.DateProperty(auto_now_add = True)
votes = db.IntegerProperty()
answers = db.StringListProperty()
tags = db.StringProperty()
问题在于,当我转到仪表板并尝试从那里创建实体时,答案属性不存在。
有没有更好的方法来获得字符串列表,所以我可以单独操作它们?
更新
当我尝试更新实体并在字符串列表中添加内容时:
链接是localhost:9082 / questions-4889528208719872
class QuestionPageHandler(BaseHandler):
def get(self, *a, **kw):
sURL = self.request.url.split("-")
question = Questions.get_by_id(long(sURL[-1]))
self.render_content("questionpage.html",question=question)
def post(self, *a, **kw):
answer = self.request.get("answer")
sURL = self.request.url.split("-")
question = Questions.get_by_id(long(sURL[-1]))
question.answers.append(answer)
question.put() **<---- I forgot to add this EDIT**
然后在html上我用这个:
{% for answer in question.answers %}
<div class="well span7">
<p>{{answer}}</p>
</div>
{% endfor %}
但我有一个空页。
答案 0 :(得分:2)
解决方案:
def post(self, *a, **kw):
answer = self.request.get("answer")
sURL = self.request.url.split("-")
question = Questions.get_by_id(long(sURL[-1]))
question.answers.append(answer)
**question.put()** <-- add this
答案 1 :(得分:1)
也许尝试具有列表类型属性的NDB
看看 - &gt;
ndb.StringProperty(重复= TRUE)
或
ndb.StructuredProperty(XXX,重复= TRUE)