2010-08-27 55 views
8

因此,我决定改写我的图片库,因为它有新的高性能图片服务。这意味着使用我以前从未使用过的Blobstore。这似乎很简单,直到我试图将BlobKey存储在我的模型中。在应用程序引擎中将BlobKey存储到DataStore中

我该如何在模型中存储对blobstorekey的引用?我应该使用字符串还是应该使用一些我不知道的特殊属性?我有这个模型

class Photo(db.Model): 
date = db.DateTimeProperty(auto_now_add=True) 
title = db.StringProperty() 
blobkey = db.StringProperty() 
photoalbum = db.ReferenceProperty(PhotoAlbum, collection_name='photos') 

而且我得到这个错误:房产的BlobKey必须是海峡或Unicode实例,而不是一类BlobKey

当然,我在应用程序引擎是一个新手,但是这是第一个大墙我已经打了。 已经广泛搜索没有任何成功。

回答

1

取而代之的是db.StringProperty的(),你需要使用db.blobstore.BlobReferenceProperty(我认为)

我仍然在试图弄清楚这件事情出来为好,但想到我会发布一些想法。

以下是参考页从谷歌: http://code.google.com/appengine/docs/python/datastore/typesandpropertyclasses.html

http://code.google.com/appengine/docs/python/datastore/typesandpropertyclasses.html#BlobReferenceProperty

+0

我要在这部分在劳动节周末工作..这就是说,如果我的妻子没有通过窗外的笔记本电脑(星期六结婚!)。 – Sologoub 2010-08-30 15:17:02

11

我下面的作品。请注意,该类是blobstore.blobstore,而不仅仅是blobstore。

型号:

from google.appengine.ext.blobstore import blobstore 

class Photo(db.Model): 
    imageblob = blobstore.BlobReferenceProperty() 

设置属性:

from google.appengine.api import images 
from google.appengine.api import blobstore 
from google.appengine.ext.webapp import blobstore_handlers 

class UploadHandler(blobstore_handlers.BlobstoreUploadHandler): 
    def post(self): 
    upload_files = self.get_uploads('file') # 'file' is file upload field in the form 
    blob_info = upload_files[0] 
    entity = models.db.get(self.request.get('id')) 
    entity.imageblob = blob_info.key() 

取得属性:

image_url = images.get_serving_url(str(photo.imageblob.key())) 
+0

谢谢你那位先生。将尝试一下。 – 2010-09-06 15:27:40

相关问题