使用应用程序引擎在数据存储中存储BlobKey

时间:2010-08-27 20:52:24

标签: google-app-engine

所以我决定重写我的图片库,因为新的高性能图像服务的东西。这意味着使用我以前从未使用过的Blobstore。在我尝试将BlobKey存储在我的模型中之前,它似乎很简单。

我如何在模型中存储对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必须是str或unicode实例,而不是BlobKey

当然,我是应用程序引擎的新手,但这是我打过的第一个主要墙。 谷歌广泛搜索没有任何成功。

2 个答案:

答案 0 :(得分: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()))

答案 1 :(得分:1)

而不是db.StringProperty()你需要使用db.blobstore.BlobReferenceProperty(我认为)

我仍然试图解决这个问题,但我想我会发表一些想法。

以下是Google的参考页面: http://code.google.com/appengine/docs/python/datastore/typesandpropertyclasses.html

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

相关问题