尝试将字符串属性设置为模型另一部分的 md5 的默认值
我想做的是在将图像存储到数据存储时即时执行,我想用图像的 md5sum 填充一个条目,以便稍后在输出图像时使用。我觉得这是一个虚拟错误,我已经忽略了,但现在看不到。
代码片段:
import hashlib
from google.appengine.api import urlfetch
from google.appengine.ext import db
def getimg(url):
result = urlfetch.fetch(url)
if result.status_code == 200:
return result.content
else:
return None
class ImageBlobs(db.Model):
img = db.BlobProperty(default=None)
last_update = db.DateTimeProperty(auto_now=True)
etag = db.StringProperty(default=hashlib.md5(img).hexdigest())
img = ImageBlobs(img=getimg('http://www.a-real-domain.com/a-working-image.png'))
img.put()
错误:
TypeError: must be convertible to a buffer, not BlobProperty
关于如何解决我的小问题的想法/建议?
What I am trying to do is on the fly when storing an image to the datastore, I would like to fill in a entry with the md5sum of the image for use later when outputting the image. I feel like it is a dummy error that I have over looked and just cant see right now.
Code snippet:
import hashlib
from google.appengine.api import urlfetch
from google.appengine.ext import db
def getimg(url):
result = urlfetch.fetch(url)
if result.status_code == 200:
return result.content
else:
return None
class ImageBlobs(db.Model):
img = db.BlobProperty(default=None)
last_update = db.DateTimeProperty(auto_now=True)
etag = db.StringProperty(default=hashlib.md5(img).hexdigest())
img = ImageBlobs(img=getimg('http://www.a-real-domain.com/a-working-image.png'))
img.put()
The error:
TypeError: must be convertible to a buffer, not BlobProperty
Ideas/Suggestions on what I can do to solve my little problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
默认值不是这样工作的。您需要一个 DerivedProperty:
Defaults don't work that way. You need a DerivedProperty: