New attachment handling in couchdb-python module

Tuesday, September 09, 2008

I've been trying out the new methods for the Database class in couchdb-python, in the svn repository.

I tried put_attachment.

Guess you already have a couchdb database called blog. To get a reference to the db you have to...

from couchdb.schema import *

class Post(Document):
author = TextField()
subject = TextField()
content = TextField()
tags = ListField( TextField() )
comments = ListField( DictField(
comment_author = TextField(),
comment = TextField(),
comment_date = DateTimeField()
date = DateTimeField()

from couchdb import Server
from datetime import datetime
import binascii
s = Server("")
blog = s["blog"]
p = Post( author = "Me", subject = "Whatever for the subject ", content = "Any content",date =, tags = ["Python", "Couchdb", "Blog"])

f = open("", "rb")
foo = binascii.b2a_base64( # this convert the content of the file to a encode 64 string

#put_attachment only works right now with an encoded string as I see and with a dictionary object for the document ( first parameter ) and not for a Document instance as used here , so...

blog = s["blog"]
adoc = blog[]

and finally call the method...

blog.put_attachment( adoc, "", foo, "text/python")

#you can't do blog.put_attachment( p, "", f, "text/python") which I taught by reading the doc inside the method.