mongoengine.errors.InvalidQueryError的原因

时间:2014-08-24 00:30:24

标签: python mongoengine flask-mongoengine

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

关于此错误的原因可能是什么?提前感谢您的投入!

2 个答案:

答案 0 :(得分:1)

此错误的原因实际上非常简单 - 正确检索对象,但Flask Web应用程序无法在前端接收纯JSON。

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

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

答案 1 :(得分:0)

我知道它很晚但可能会帮助别人。你可以这样做:

return jsonify(bingo=bingo)

不要忘记导入jsonify