问题
The journey with Flask, MongoDB and MongoEngine continues.
I've (hopefully) synced up the database with my model in a normal fashion, yet when I attempt to query the database for something as simple as an address name, I get this message:
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)
I've also gotten this error (that may be related) from poking around in the 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.
Here's my code for models.py, linked up properly to 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
And here's where I call get_person_from_db(...)
, the function in question:
@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
Any ideas as to what the cause of this error may be? Thanks in advance for the input!
回答1:
The cause of this error was actually surprisingly simple – the object was being retrieved correctly, but the Flask web application is not amenable to receiving pure JSON on the front end.
For some reason I expected it would print the JSON as a string, but that was not the case.
In order to get the desired outcome, one could call field names specifically (specific_person.name
) or using Flask's render_template
functionality to format the output nicely on a webpage.
回答2:
I know it's VERY late but might help someone else. you can do something like:
return jsonify(bingo=bingo)
Don't forget to import jsonify
though.
来源:https://stackoverflow.com/questions/25467559/cause-of-mongoengine-errors-invalidqueryerror