【问题标题】:Storing BlobKey in DataStore with app engine使用应用引擎将 BlobKey 存储在 DataStore 中
【发布时间】:2010-08-27 20:52:24
【问题描述】:

所以我决定重写我的图片库,因为新的高性能图片服务的东西。这意味着使用我以前从未使用过的 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

诚然,我是应用引擎方面的新手,但这是我遇到的第一堵大墙。 谷歌搜索广泛没有任何成功。

【问题讨论】:

    标签: google-app-engine


    【解决方案1】:

    以下内容对我有用。请注意,该类是 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()))
    

    【讨论】:

    • 谢谢先生。会试试看。
    【解决方案2】:

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

    我仍在尝试解决这个问题,但我想我会发布一些想法。

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

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

    【讨论】:

    • 我将在劳动节周末从事这部分工作......也就是说,如果我妻子不通过窗外的笔记本电脑(周六结婚!)。
    猜你喜欢
    • 1970-01-01
    • 2012-11-17
    • 2012-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-26
    • 2015-08-03
    • 1970-01-01
    相关资源
    最近更新 更多