2

Flask、MongoDB 和 MongoEngine 的旅程仍在继续。

我(希望)以正常方式将数据库与我的模型同步,但是当我尝试查询数据库以获取像地址名称这样简单的东西时,我收到以下消息:

mongoengine.errors.InvalidQueryError
InvalidQueryError: Not a query object: {'wsgi.multiprocess': False, 'SERVER_SOFTWARE': 
'Werkzeug/0.9.6', 'SCRIPT_NAME': '', 'REQUEST_METHOD': 'GET', 'PATH_INFO': '/result', 
'SERVER_PROTOCOL': 'HTTP/1.1', 'QUERY_STRING': 'query=28+Paritai+Drive+Orakei', 
'werkzeug.server.shutdown': <function shutdown_server at 0x10f8cd1b8>, 'CONTENT_LENGTH': '', 
'HTTP_USER_AGENT': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_4) AppleWebKit/537.36 (KHTML, like 
Gecko) Chrome/36.0.1985.143 Safari/537.36', 'HTTP_CONNECTION': 'keep-alive', 'SERVER_NAME': 
'127.0.0.1', 'REMOTE_PORT': 60329, 'wsgi.url_scheme': 'http', 'SERVER_PORT': '5000', 
'werkzeug.request': <Request 'http://localhost:5000/result?query=28+Paritai+Drive+Orakei' [GET]>, 
'wsgi.input': <socket._fileobject object at 0x10f8932d0>, 'HTTP_HOST': 'localhost:5000', 
'wsgi.multithread': False, 'HTTP_CACHE_CONTROL': 'max-age=0', 'HTTP_ACCEPT': 
'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8', 'wsgi.version': (1, 
0), 'wsgi.run_once': False, 'wsgi.errors': <open file '<stderr>', mode 'w' at 0x10e8581e0>, 
'REMOTE_ADDR': '127.0.0.1', 'HTTP_ACCEPT_LANGUAGE': 'en-US,en;q=0.8', 'CONTENT_TYPE': '', 
'HTTP_ACCEPT_ENCODING': 'gzip,deflate,sdch'}.
Did you intend to use key=value?

Traceback (most recent call last)

我也从 Python shell 中得到了这个错误(可能是相关的):

>>> db.properties.objects()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Library/Python/2.7/site-packages/pymongo/collection.py", line 1705, in __call__
self.__name)
TypeError: 'Collection' object is not callable. If you meant to call the 'objects' method on a     
'Database' object it is failing because no such method exists.

这是我的 models.py 代码,正确链接到 Flask:

from app import db

# ----------------------------------------
# Taking steps towards a working backend.
# ----------------------------------------

class Person(db.Document):

    # Meta variables.
    meta = {
        'collection': 'properties'
    }

    # Document variables.
    name = db.StringField(max_length=255, required=True)
    address =  db.StringField(max_length=255, required=True)
    email = db.StringField(max_length=255, required=True)

    def __repr__(self):
        return address

    def get_person_from_db(self, query_string):
        if not query_string:
            raise ValueError()
        # Ultra-simple search for the moment.
        person_class_object = Property
        bingo = person_class_object.objects(__raw__={'name': query_string})
        return bingo

这就是我所说get_person_from_db(...)的,有问题的函数:

@app.route('/result')
def result():
search_string = request.args['query']
if search_string == '':
    return render_template('index.html')
else:

    person_class_object = models.Person()
    specific_person = person_class_object.get_person_from_db(search_string)

    return specific_person

关于此错误的原因可能是什么的任何想法?提前感谢您的输入!

4

2 回答 2

1

这个错误的原因实际上非常简单——对象被正确检索,但 Flask Web 应用程序不适合在前端接收纯 JSON。

出于某种原因,我希望它将 JSON 打印为字符串,但事实并非如此。

为了获得所需的结果,可以专门调用字段名称 ( specific_person.name) 或使用 Flask 的render_template功能在网页上很好地格式化输出。

于 2014-08-26T21:13:38.230 回答
0

我知道现在很晚,但可能会帮助其他人。您可以执行以下操作:

return jsonify(bingo=bingo)

不过不要忘记导入jsonify

于 2017-09-28T09:15:01.707 回答