Class ViewField
object --+
|
ViewField
Descriptor that can be used to bind a view definition to a property of
a Document class.
>>> class Person(Document):
... name = TextField()
... age = IntegerField()
... by_name = ViewField('people', '''\
... function(doc) {
... emit(doc.name, doc);
... }''')
>>> Person.by_name
<ViewDefinition '_design/people/_view/by_name'>
>>> print Person.by_name.map_fun
function(doc) {
emit(doc.name, doc);
}
That property can be used as a function, which will execute the view.
>>> from couchdb import Database
>>> db = Database('http://localhost:5984/python-tests')
>>> Person.by_name(db, count=3)
<ViewResults <PermanentView '_design/people/_view/by_name'> {'count': 3}>
The results produced by the view are automatically wrapped in the
Document subclass the descriptor is bound to. In this example, it would
return instances of the Person
class. But please note that this requires
the values of the view results to be dictionaries that can be mapped to the
mapping defined by the containing Document class. Alternatively, the
include_docs query option can be used to inline the actual documents in
the view results, which will then be used instead of the values.
If you use Python view functions, this class can also be used as a
decorator:
>>> class Person(Document):
... name = TextField()
... age = IntegerField()
...
... @ViewField.define('people')
... def by_name(doc):
... yield doc['name'], doc
>>> Person.by_name
<ViewDefinition '_design/people/_view/by_name'>
>>> print Person.by_name.map_fun
def by_name(doc):
yield doc['name'], doc
|
__init__(self,
design,
map_fun,
reduce_fun=None,
name=None,
language=' javascript ' ,
wrapper=DEFAULT,
**defaults)
Initialize the view descriptor. |
|
|
|
__get__(self,
instance,
cls=None) |
|
|
Inherited from object :
__delattr__ ,
__format__ ,
__getattribute__ ,
__hash__ ,
__new__ ,
__reduce__ ,
__reduce_ex__ ,
__repr__ ,
__setattr__ ,
__sizeof__ ,
__str__ ,
__subclasshook__
|
|
define(cls,
design,
name=None,
language=' python ' ,
wrapper=DEFAULT,
**defaults)
Factory method for use as a decorator (only suitable for Python
view code). |
|
|
Inherited from object :
__class__
|
__init__(self,
design,
map_fun,
reduce_fun=None,
name=None,
language=' javascript ' ,
wrapper=DEFAULT,
**defaults)
(Constructor)
|
|
Initialize the view descriptor.
- Parameters:
design - the name of the design document
map_fun - the map function code
reduce_fun - the reduce function code (optional)
name - the actual name of the view in the design document, if
it differs from the name the descriptor is assigned to
language - the name of the language used
wrapper - an optional callable that should be used to wrap the
result rows
defaults - default query string parameters to apply
- Overrides:
object.__init__
|