我正在用Python(3.5)和flask(1.1.1)对CouchDB数据库(2.3.1)编写一些dockerized代码,使用cloudant Python扩展(2.12.0),它似乎是对CouchDB最新的库。你知道吗
我正在尝试从数据库中获取和使用视图,但它不起作用。我可以提取文档,并正常使用数据库,但我不能使用视图。你知道吗
我为该对象添加了一个print语句,该语句应该在程序启动时保存设计文档,并且我看到该文档显示为没有视图(或任何视图),CouchDB日志显示没有对正在进行的设计文档的请求。你知道吗
我还尝试使用相同的URL和用户名/密码获取设计文档并通过curl使用视图,这两个操作都成功地工作了。你知道吗
下面是失败的示例代码:
from flask import Flask, render_template , request, g
from cloudant.client import CouchDB
from cloudant.view import View
from cloudant.design_document import DesignDocument
import requests
application = Flask(__name__)
application.config.from_pyfile("config.py")
couch = CouchDB(application.config['COUCHDB_USER'], application.config['COUCHDB_PASSWORD'], url=application.config['COUCHDB_SERVER'], connect=True, auto_renew=True)
database = couch[application.config['COUCHDB_DATABASE']]
views = DesignDocument(database, '_design/vistas')
print(views)
print(views.list_views())
@application.route("/", methods=['GET', 'POST'])
def index():
for pelicula in View(views,'titulos_peliculas'):
titulos.append({ "id": pelicula['id'], "titulo": pelicula['key'] })
return render_template('menu.html',titulos=titulos)
在该代码中,设计文档(视图)的打印返回:
{'lists': {}, 'indexes': {}, 'views': {}, 'shows': {}, '_id': '_design/vistas'}
显示空视图。。。CouchDB日志只显示登录数据库并获取数据库信息:
couchdb:5984 172.23.0.4 undefined POST /_session 200 ok 69
couchdb:5984 172.23.0.4 vmb_web HEAD //peliculas 200 ok 232
完全没有其他查询。你知道吗
应用程序日志中也没有错误。即使我称之为视图的路由使用:
[pid: 21|app: 0|req: 1/1] 172.23.0.1 () {52 vars in 1225 bytes} [Mon Aug 5 15:03:24 2019] POST / => generated 1148 bytes in 56 msecs (HTTP/1.1 200) 2 headers in 81 bytes (1 switches on core 0)
如我所说,我可以得到并使用文件:
curl http://vmb_web:password@127.0.0.1:999/peliculas/_design/vistas
{"_id":"_design/vistas","_rev":"1-e8108d41a6627ea61b9a89a637f574eb","language":"javascript","views":{"peliculas":{"map":"function(doc) { if (doc.schema == 'pelicula') { emit(doc.titulo, null); for(i=0;i<doc.titulos_alt.length;i++) { emit(doc.titulos_alt[i],null); } for(i=0;i<doc.directores.length;i++) { emit(doc.directores[i].nombre,null); } for(i=0;i<doc.actores.length;i++) { emit(doc.actores[i].nombre,null); } for(i=0;i<doc.escritores.length;i++) { emit(doc.escritores[i].nombre,null); } for(i=0;i<doc.etiquetas.length;i++) { emit(doc.etiquetas[i],null); } } }"},"titulos_peliculas":{"map":"function(doc) { if ((doc.schema == 'pelicula') && (doc.titulo)) { emit(doc.titulo, null); } }"},"archivos_peliculas":{"map":"function(doc) { if ((doc.schema == 'pelicula') && (doc.titulo)) { emit(doc.titulo, doc.archivo); } }"},"titulo_rev":{"map":"function(doc) { if ((doc.schema == 'pelicula') && (doc.titulo)) { emit(doc.titulo, doc._rev); } }"}}}
我相信上面发布的代码会创建一个新的DesignDocument对象,而不会搜索现有的DesignDocument。在创建该对象之后,您似乎需要调用它的fetch()方法,然后检查它的views属性。嗯。你知道吗
附言:把我的评论推广到一个答案,希望这几天在SO land很酷:)
我在回答我自己的问题,以防将来有人偶然发现。我在pythoncloudant的github中从Esteban Laver那里得到了答案,这就是@chrisinmtown在上面的回复中提到的。你知道吗
在使用设计文档之前,我未能对其调用fetch()。你知道吗
另一个很好的建议是对数据库对象使用get\u view\u result helper方法,该方法负责一次从选定的视图中获取设计文档和实例化视图对象。你知道吗
相关问题 更多 >
编程相关推荐