Insert information on file in GridFS

37 views
Skip to first unread message

Hélio Miranda

unread,
May 21, 2013, 11:25:55 AM5/21/13
to mongoeng...@googlegroups.com
Hi!

I have my application in Django and MongoDB, where I am inserting files in GridFS.
My question is whether it is possible to insert a caption for example insert a picture in which I am entering in GridFS and then retrieve this legend when I retrieve the picture?
You can do this?

If so, any examples available?

Ross Lawley

unread,
May 22, 2013, 6:40:24 AM5/22/13
to mongoeng...@googlegroups.com
Hi!

Yes when you put the file you can save meta data.  In this test we also set foo="bar":


Ross


--
You received this message because you are subscribed to the Google Groups "MongoEngine Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongoengine-us...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Hélio Miranda

unread,
May 22, 2013, 6:54:15 AM5/22/13
to mongoeng...@googlegroups.com
okay
I'm doing the following to insert the files in GridFS:
def upload(request, id):
    if request.method == 'POST':
        movie = Movie.objects(id=id)
        my_painting = Movie(id=id)
     
        files = []
        for f in request.FILES.getlist('file'):
           mf = mongoengine.fields.GridFSProxy()
           mf.put(f, filename=f.name, legend='Oi')
           files.append(mf)
           my_painting.MovieCover = files
           my_painting.save()


And I'm going to do this to recover the files:
def view(request, id):
    fs = mongoengine.fields.GridFSProxy()
    foto = fs.get(id=ObjectId(id)).read()
    
    return HttpResponse(foto, content_type='image/jpeg')

If I recover the legend as you want to enter as I do?

Ross Lawley

unread,
May 22, 2013, 7:41:31 AM5/22/13
to mongoeng...@googlegroups.com
Hi,

You seem to be bypassing the MongoEngine Document and just using the GridFSProxy - if that is the case then why not just use vanilla gridfs from pymongo?

In terms of getting metadata you can get that from the grid_fs eg:

   foto = fs.get(id=ObjectId(id))
   legend = foto.legend
   filename = foto.filename
   file_contents = foto.read()

Ross


--
Reply all
Reply to author
Forward
0 new messages