views:

119

answers:

2

So I decided to rewrite my image gallery because of the new high performance image serving thing. That meant using Blobstore which I have never used before. It seemed simple enough until I tried to store the BlobKey in my model.

How on earth do I store reference to a blobstorekey in a Model? Should I use string or should I use some special property that I don't know about? I have this model

class Photo(db.Model):
 date = db.DateTimeProperty(auto_now_add=True)
 title = db.StringProperty()
 blobkey = db.StringProperty()
 photoalbum = db.ReferenceProperty(PhotoAlbum, collection_name='photos') 

And I get this error: Property blobkey must be a str or unicode instance, not a BlobKey

Granted, I am a newbie in app engine but this is the first major wall I have hit yet. Have googled around extensively without any success.

+1  A: 

Instead of a db.StringProperty() you need to use db.blobstore.BlobReferenceProperty (I think)

I'm still trying to figure this thing out as well, but thought I'd post some ideas.

Here are the reference pages from Google: http://code.google.com/appengine/docs/python/datastore/typesandpropertyclasses.html

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

Sologoub
I went another way with this. I Just store the url I get from get_serving_url(blobkey). Still would like to know how this is done though.
Símon Óttar Vésteinsson
I'm going to work on this part over the labor day weekend... that is if my wife doesn't through the laptop out the window (getting married on saturday!).
Sologoub
+4  A: 

The following works for me. Note the class is blobstore.blobstore instead of just blobstore.

Model:

from google.appengine.ext.blobstore import blobstore

class Photo(db.Model):
  imageblob = blobstore.BlobReferenceProperty()

Set the property:

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()

Get the property:

image_url = images.get_serving_url(str(photo.imageblob.key()))
Kenan
Thank you for that sir. Will try that out.
Símon Óttar Vésteinsson