1. Sure, in same way as any other field.
2. No, it will be static because you have to define design and view
name for ViewField and sync it with database or ResourceNotFound
exception will be raised when you'll get the results.
--
,,,^..^,,,
Technically it could be (cache hit by function code hash), but in real
world it could not and you shouldn't relay on this. For small
databases this is not a problem, but not for big ones.
--
,,,^..^,,,
No problem(: I'll repost my answer:
Sure, just pass include_doc=True parameter to view call and wrap
document from row['doc'] by related class. For example:
import couchdb
from couchdb.mapping import *
class User(Document):
name = TextField()
type = TextField(default='user')
class Article(Document):
author = TextField()
type = TextField(default='article')
@ViewField.define('article',
include_docs=True,
wrapper=lambda row: User(**row['doc']))
def by_user(doc):
if doc['type'] == 'article':
yield doc['_id'], {'_id': doc['author']}
db = couchdb.Database('http://localhost:5984/blog')
for user in Article.by_user(db):
assert isinstance(user, User)
assert user.name is not None
# assert user.name == 'bar'
--
,,,^..^,,,
This lambda runs on couchdb-python side to handle rows from views
result. If you remove it, next code will do the same thing:
for row in Article.by_user(db):
user = User(**row['doc'])
For JS those ViewField will looks like:
MAP_FUN = """
function(doc){
if (doc.type == 'article'){
emit(doc._id, {'_id': doc.author})
}
}
"""
class Article(Document):
author = TextField()
type = TextField(default='article')
by_user = ViewField('article',
map_fun=MAP_FUN,
include_docs=True,
wrapper=lambda row: User(**row['doc']))
P.S. `by_user` name wasn't good because actually you'll get an authors
list, not articles by some user id.
--
,,,^..^,,,