2013-05-09 54 views
1

我想創建一個谷歌應用程序引擎數據庫具有以下屬性谷歌的數據庫

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() 

應用程序引擎的StringList屬性的問題是,當我去到儀表板,並試圖從那裏創建一個實體,答案屬性不存在。

有沒有更好的方法來獲得一個字符串列表,所以我可以單獨操作它們?

更新:

當我嘗試更新的實體,並添加一些字符串列表:

鏈接爲localhost:9082號文件/問題-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** 

,然後在我用這個:

{% for answer in question.answers %} 
    <div class="well span7"> 
     <p>{{answer}}</p> 
    </div> 
{% endfor %} 

但我有一個空的頁面。

+1

對於超過最基本的類型,您將無法使用數據存儲區查看器。我建議你使用remote_api_shell或爲你的應用程序編寫一些代碼。 – 2013-05-09 09:33:10

+0

您能否看到更新?謝謝 – Tasos 2013-05-09 09:41:22

+0

什麼是BaseHandler。如果你使用webapp2你通常self.response.write self.render_content做什麼, 它調用self.response.write? – 2013-05-09 09:44:55

回答

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

Link to NDB

也許嘗試NDB具有列表類型的屬性

看看 - >

ndb.StringProperty(重複= TRUE) 或
ndb.StructuredProperty(xxx,重複= True)