2010-05-23 28 views
5

似乎StringListProperty只能包含字符串高達每500個字符,就像StringProperty ...StringListProperty限制在500個字符字符串(谷歌應用程序引擎/ Python)的

有沒有一種方法來存儲長於串?我不需要他們被索引或任何東西。我需要的將是類似於「TextListProperty」的東西,其中列表中的每個字符串可以是任意長度並且不限於500個字符。

我可以創建一個屬性嗎?或者你可以專家建議一種不同的方法?也許我應該使用普通列表並在Blob字段中醃製/取消它,或者類似的東西?我對Python和GAE有點新,我會非常感謝一些指針,而不是在試驗和錯誤上花費幾天時間......謝謝!

回答

2

您可以根據需要使用通用ListPropertyitem_typestrunicode或其他)。

+2

兩個海峽和unicode也被限制在500個字符。但後來我試着用db.Text作爲一種類型,到目前爲止它似乎工作。感謝您指點我正確的方向。 – MarcoB 2010-05-23 21:28:42

4

亞歷克斯已經回答前不久,但如果別人來用了同樣的問題一起:

你只讓item_type等於db.Text(如OP在評論中提到)。
這裏有一個簡單的例子:

from google.appengine.ext import db 
class LargeTextList(db.Model): 
    large_text_list = db.ListProperty(item_type=db.Text) 

def post(self): 
    # get value from a POST request, 
    # split into list using some delimiter 
    # add to datastore 
    L = self.request.get('large_text_list').split() # your delimiter here 
    LTL = [db.Text(i) for i in L] 
    new = LargeTextList() 
    new.large_text_list = LTL 
    new.put() 

def get(self): 
    # return one to make sure it's working 
    query = LargeTextList.all() 
    results = query.fetch(limit=1) 
    self.render('index.html', 
      { 'results': results, 
       'title': 'LargeTextList Example', 
      }) 
+1

感謝您的代碼,這是要走的路。要記住的一件事是每個賦值都必須對db.Text進行類型轉換,否則編譯器會拋出異常。甚至可以分配一個空字符串,例如: object.text_list.append(db.Text(「」)) – MarcoB 2010-05-24 14:40:29

相關問題