ndb and blobstore

65 views
Skip to first unread message

Pau

unread,
Dec 18, 2013, 6:04:37 PM12/18/13
to appengine-...@googlegroups.com
Hi,

I have this model:

class Product(ndb.Model):
    title = ndb.StringProperty()
    description = ndb.TextProperty()
    img = ndb.BlobKeyProperty(indexed=False)

I need a html form that reads the values ​​of fields (title and description) and read the image (from a file field), and keeps the values ​​NDB object, the image in the Blobstore and updates the field BlobKeyProperty correctly.

As I work with wtforms, I tried to do it with a form like the following:

class ProductForm(Form):
    title = fields.TextField('Title', [validators.Required(), validators.Length(min=4, max=25)])
    description = fields.TextAreaField('Description')
    img = fields.FileField('img')

The form shows the file field correctly but in the POST, it does not work because I don't know how to read the file, save the file to Blobstore and update the BlobKeyProperty.

My handler is this:

class ProductHandler(BaseHandler):
    def new(self):
        if self.request.POST:
            data = ProductForm(self.request.POST)
            if data.validate():
                model = Product()
                data.populate_obj(model)
                model.put()
                self.add_message("Product add!", 'success')
                return self.redirect_to("product-list")
            else:
                self.add_message("Product not add!", 'error')
        params = {
            'form': ProductForm(),
            "kind": "product",
        }
        return self.render_template('admin/new.html', **params)

Error is Expected str, got u'image.jpg'

If someone can help me, I would appreciate it!

Guido van Rossum

unread,
Dec 18, 2013, 6:14:18 PM12/18/13
to appengine-...@googlegroups.com
You might get a quicker answer on StackOverflow.
> --
> You received this message because you are subscribed to the Google Groups
> "appengine-ndb-discuss" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to appengine-ndb-di...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.



--
--Guido van Rossum (python.org/~guido)

Pau

unread,
Dec 19, 2013, 4:20:09 AM12/19/13
to appengine-...@googlegroups.com
Thanks Guido.

I also posted the question on StackOverflow. See if there lucky.

Pau



2013/12/19 Guido van Rossum <gu...@python.org>

Pau

unread,
Dec 20, 2013, 7:55:52 PM12/20/13
to appengine-...@googlegroups.com

The only solution I found is to use a deprecated low-level API described in https://developers.google.com/appengine/docs/python/blobstore/#Python_Writing_files_to_the_Blobstore

I make a wtform validator for the FileField.

I changed:

img = fields.FileField('img')

To:

img = fields.FileField('img', [create_upload_file])

And I write this validator:

def create_upload_file(form, field):
    file_name = files.blobstore.create(mime_type=field.data.type, _blobinfo_uploaded_filename=field.data.filename)
    with files.open(file_name, 'a') as f:
        f.write(field.data.file.read())
    files.finalize(file_name)
    blob_key = files.blobstore.get_blob_key(file_name)
    field.data = blob_key

The validator create the blob in blobstore, and then it change the field data from FieldStorage to blob_key.

I don't think that is the best solution but it works for now.


Patrick Costello

unread,
Dec 20, 2013, 8:19:38 PM12/20/13
to appengine-...@googlegroups.com
If you want to avoid using the deprecated low-level API, you should upload the files to Google Cloud Storage instead: https://developers.google.com/appengine/docs/python/googlecloudstorageclient/. You can still serve them using blobstore: https://developers.google.com/appengine/docs/python/blobstore/#Python_Using_the_Blobstore_API_with_Google_Cloud_Storage.



Pau

unread,
Dec 21, 2013, 4:39:27 AM12/21/13
to appengine-...@googlegroups.com
For now, I prefer to use the BlobStore because there are 5GB of free storage. and I have enough.
I guess in the future I will have to migrate to GCS, as you suggest me. But I see that it is a simple process.

Thanks for your reply and the links!

Pau

Jakob Holmelund

unread,
Dec 21, 2013, 7:46:18 AM12/21/13
to appengine-...@googlegroups.com

Def go with gcs. Totally fixed uploading on appengine and if you are under 5gb it will be dirt cheap anyways

Pau

unread,
Jan 2, 2014, 3:36:20 AM1/2/14
to appengine-...@googlegroups.com
Thanks Jakob for your advices!

2013/12/21 Jakob Holmelund <jakobho...@gmail.com>
Reply all
Reply to author
Forward
0 new messages