couchdb.schema

Package couchdb :: Module schema |

# Module schema

Mapping from raw JSON data structures to Python objects and vice versa.

    >>> from couchdb import Server
    >>> server = Server('http://localhost:5984/')
    >>> db = server.create('python-tests')

To define a document schema, you declare a Python class inherited from Document, and add any number of Field attributes:

    >>> class Person(Document):
    ...     name = TextField()
    ...     age = IntegerField()
    ...     added = DateTimeField(default=datetime.now)
    >>> person = Person(name='John Doe', age=42)
    >>> person.store(db) #doctest: +ELLIPSIS
    <Person ...>
    >>> person.age
    42

You can then load the data from the CouchDB server through your Document subclass, and conveniently access all attributes:

    >>> person = Person.load(db, person.id)
    >>> old_rev = person.rev
    >>> person.name
    u'John Doe'
    >>> person.age
    42
    >>> person.added                #doctest: +ELLIPSIS
    datetime.datetime(...)

To update a document, simply set the attributes, and then call the `store()` method:

    >>> person.name = 'John R. Doe'
    >>> person.store(db)            #doctest: +ELLIPSIS
    <Person ...>

If you retrieve the document from the server again, you should be getting the updated data:

    >>> person = Person.load(db, person.id)
    >>> person.name
    u'John R. Doe'
    >>> person.rev != old_rev
    True

    >>> del server['python-tests']

Classes
| Field Basic unit for mapping a piece of data between Python and JSON.
| Schema
| Document
| TextField Schema field for string values.
| FloatField Schema field for float values.
| IntegerField Schema field for integer values.
| LongField Schema field for long integer values.
| BooleanField Schema field for boolean values.
| DecimalField Schema field for decimal values.
| DateField Schema field for storing dates.
| DateTimeField Schema field for storing date/time values.
| TimeField Schema field for storing times.
| DictField Field type for nested dictionaries.
| ListField Field type for sequences of other fields.