-1

I'm using Flask and CouchDB. I've mapped a file document to a file object.

class File(Document):
   name = TextField()
   conten = TextField()

In my Flask Blueprint I want to return all files. How do I do that. I know that I've to write a view like:

function(doc) {
   if(doc.type == 'file') {
      emit(doc_id, doc)
   }
}

But where do I've to put this piece of code? In my File object? Do I've to add this view definition to the CouchDBManager? Or should I use the query function?

Paco
  • 4,520
  • 3
  • 29
  • 53
OrangeTux
  • 11,142
  • 7
  • 48
  • 73

1 Answers1

0

Don't use the query function, that is just for temporary views, i.e. not for production use. Instead you should create an object of class ViewDefinition and pass that object to the CouchDBManager.add_viewdef method. This is described in a bit more detail on http://pythonhosted.org/Flask-CouchDB/#writing-views.

Alternatively, you can directly use the CouchDB http interface to upload the design document. As you have to create the view only once, this would still be feasible. Practically, you would open futon on http://localhost:5984/_utils and create a design document with the view in your database. More information about that on http://guide.couchdb.org/draft/design.html.

brdlph
  • 619
  • 4
  • 15
  • If done it the way you described it at first. But how do create a ViewDefinition in a blueprint? – OrangeTux Apr 10 '13 at 10:58
  • I am not sure if I understand your problem correctly, but to create a ViewDefinition object you would just call this: ViewDefinition("", "", ""). The view code you listed in your question will then list all documents with the field type equal to "file". – brdlph Apr 10 '13 at 12:53