Skip to content
Browse files

Authenticate to the database

If both `MONGODB_USERNAME` and `MONGODB_PASSWORD` are set, authenticate
the connection. Also use Database as described in the documentation.

ref: http://api.mongodb.org/python/current/api/pymongo/database.html
  • Loading branch information...
1 parent a1410d5 commit 1a60c890a85ce2ee7f1bd142eb1916b76eaa8d47 @ferrix ferrix committed
Showing with 8 additions and 3 deletions.
  1. +8 −3 tastypie_mongodb/resources.py
View
11 tastypie_mongodb/resources.py
@@ -1,5 +1,5 @@
from bson import ObjectId
-from pymongo import MongoClient
+from pymongo import MongoClient, Database
from django.core.exceptions import ImproperlyConfigured
from django.core.urlresolvers import reverse
@@ -9,11 +9,16 @@
from tastypie.resources import Resource
-db = MongoClient(
+db = Database(MongoClient(
host=getattr(settings, "MONGODB_HOST", None),
port=getattr(settings, "MONGODB_PORT", None)
-)[settings.MONGODB_DATABASE]
+), settings.MONGODB_DATABASE])
+username = getattr(settings, "MONGODB_USERNAME", None)
+password = getattr(settings, "MONGODB_PASSWORD", None)
+
+if username and password:
+ db.authenticate(username, password)
class Document(dict):
# dictionary-like object for mongodb documents.

0 comments on commit 1a60c89

Please sign in to comment.
Something went wrong with that request. Please try again.