gpt4 book ai didi

google-app-engine - 使用 App Engine 将 BlobKey 存储在 DataStore 中

转载 作者:太空宇宙 更新时间:2023-11-03 15:20:41 26 4
gpt4 key购买 nike

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

我得到这个错误:Property blobkey must be a str or unicode instance, not a BlobKey

诚然,我是 App Engine 的新手,但这是我遇到的第一堵墙。广泛搜索但没有成功。

最佳答案

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

关于google-app-engine - 使用 App Engine 将 BlobKey 存储在 DataStore 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3588042/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com