1

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?

1 Answer 1

1

Defaults don't work that way. You need a DerivedProperty:

@DerivedProperty
def etag(self):
  return hashlib.md5(self.img).hexdigest()
1
  • By the way, this won't save effort computing it at read-time - it'll just make it indexable. Sep 15, 2011 at 1:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.