cloudant-labs / cloudant-python

Asynchronous Cloudant / CouchDB interface for Python
http://cloudant-labs.github.io/cloudant-python/
37 stars 17 forks source link

Unable to create a document with auto-generated _id #52

Open durera opened 9 years ago

durera commented 9 years ago

doc = cloudantDb.document("someIdIMadeUp") future = doc.put(params={'type': foo, 'name': bar}) future.add_done_callback(callback)

I really just want to submit a record and allow Cloudant to generate the UUID. I can see that I can make a seperate request to Cloudant to get a list of UUIDs and then pass this in, but that seems like a redundant use of a HTTP call when I should just be able to submit a doc without an ID and allow couch to assign it. I can't seem to find a way to do this using this library implementation .. am I missing something obvious perhaps?

e.g. something like

doc = cloudantDb.document() future = doc.put(params={'type': foo, 'name': bar}) future.add_done_callback(myCallback)

or

future = cloudantDb.putDoc({'type': foo, 'name': bar}) future.add_done_callback(myCallback)

mgmarino commented 9 years ago

Hi, you need to POST to the DB, see the couchDB API:

https://couchdb.readthedocs.org/en/1.6.1/api/database/common.html (POST)

In cloudant-python, this looks like:

>>> import cloudant
>>> acct = cloudant.Account()
>>> acct.login("if", "necessary")
>>> db = acct["test"]
>>> db.put() # make new db
>>> resp = db.post(params={"type" : "mynewdoc"}) # Make document
>>> resp.json() # Doc ID is in the returned JSON
{u'rev': u'1-75fbf1882db2de1c6cb1222843788e2f', u'ok': True, u'id': u'0510508532c3ea085d3e8c682daa9f96'}
durera commented 9 years ago

Awesome, thanks. Seems blindingly obvious now :+1: