I have a weird error in my flask application.
Problem is that the code below works fine in the Flask development server but fails in the production server (Uwsgi+Nginx). The code’s intention is to retrieve an entire collection from mongoDb and serve it’s contents as JSON.
I am not using virtualenv as I work under a virtual machine and also, I don’t have any other projects running on that machine.
So the stack is like this:
Python
Flask
MongoDb->MongoEngine
Uwsgi
NginX
I have two models:
One for Company:
class Company(Document):
name = StringField(max_length=50)
....
One for Campaigns:
class Campaign(Document):
name = StringField(min_length=8)
company = ReferenceField(Company)
....
The routing is done like this:
@app.route('/getjson', methods=['GET', 'POST'])
def getJson():
if request.method == 'GET':
collection = request.args.get('collection')
if collection == 'Campaign':
return jsonify (getCampaign())
And the getCampaign() function goes like this:
def getCampaign():
theCollection = Campaign.objects.all()
theDict = {}
for obj in x:
dict_model = {
obj.name:
{
'company': obj.company.name,
...
}
theDict.update(dict_model)
return theDict
This results in a nicely formated Json with “application/json” as Content-Type. Problem is when o try the same code on the production server it fails miserably throwing the following error!
Traceback (most recent call last):
File "/usr/local/lib/python2.6/dist-packages/flask/app.py", line 1518, in __call__
return self.wsgi_app(environ, start_response)
File "/usr/local/lib/python2.6/dist-packages/flask/app.py", line 1506, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "/usr/local/lib/python2.6/dist-packages/flask/app.py", line 1504, in wsgi_app
response = self.full_dispatch_request()
File "/usr/local/lib/python2.6/dist-packages/flask/app.py", line 1264, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/usr/local/lib/python2.6/dist-packages/flask/app.py", line 1262, in full_dispatch_request
rv = self.dispatch_request()
File "/usr/local/lib/python2.6/dist-packages/flask/app.py", line 1248, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "./mobuy.py", line 58, in getJson
return jsonify (getCampaign())
File "./jsonOut.py", line 36, in getCampaign
'company': obj.company.name,
File "/usr/local/lib/python2.6/dist-packages/bson/dbref.py", line 88, in __getattr__
raise AttributeError(key)
AttributeError: name
Running ” ‘company’: “%s” % obj.dict ” throws this result:
"company":"{'_created': False, '_data': {'status': 3, 'startDate':
datetime.datetime(2012, 5, 23, 20, 22, 28, 42000), None: ObjectId('4fbd4704f65b813c5900000d'),
'EndDate': datetime.datetime(2012, 5, 23, 20, 22, 28, 42000),
'name': u'Campaign13', 'max_claimed': 39, 'text': u'a discount',
'company': DBRef(u'company', ObjectId('4fbd36f2f65b813869000008')),
'image': u'http://www.Company8.com/image.jpg', 'Shops': [],
'category': 5, 'id': None, 'coupons': []},
'_id': ObjectId('4fbd4704f65b813c5900000d'), '_changed_fields': [],
'_initialised': True}",
HELP! Why is this working in dev server and not working in Uwsgi??
The solution i found is, i went around the problem and queried the database with pymongo directly
Not sure if i used the pymongo query correctly but this works for now, only problem is, It’s Dirty.