1

I just started working on a project, I am using Django1.10, I wanted to use mongoDB as backend...

I tried all possible ways, django-mongo-engine requires django-nonrel1.5, but if I used it then I have to do lot of work, and its complicated...

I tried django-mongoengine also but it was only tested on django1.9, (django 1.9 not supporting admin)

So now I decided to use pymongo...

I need help How can I configure database? and How to work with django without ORM?

EDIT :

This is my setting.py file with django-mongoengine

settings.py

import os

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
SECRET_KEY = '------------'
DEBUG = True
ALLOWED_HOSTS = []

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'mongoengine',
    'LocalTeamsApp'
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'LocalTeams.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'LocalTeams.wsgi.application'

# MongoDB settings
MONGODB_DATABASES = {
    'default': {'name': 'django_mongoengine'}
}

DATABASES = {
    "default": {
        "NAME": '****',
        "PASSWORD": '****',
        "USER": '****',
        'ENGINE':'django.db.backends.dummy'
    }
}

INSTALLED_APPS += ["django_mongoengine"]


AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]

AUTH_USER_MODEL = 'mongo_auth.MongoUser'

AUTHENTICATION_BACKENDS = (
    'django_mongoengine.mongo_auth.backends.MongoEngineBackend',
)

SESSION_ENGINE = 'django_mongoengine.sessions'

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static/')

Then I fired the cmnd

python manage.py runserver

I got following error:-

Unhandled exception in thread started by <function wrapper at 0x7f8648b43ed8>
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/django/utils/autoreload.py", line 226, in wrapper
    fn(*args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/commands/runserver.py", line 113, in inner_run
    autoreload.raise_last_exception()
  File "/usr/local/lib/python2.7/dist-packages/django/utils/autoreload.py", line 249, in raise_last_exception
    six.reraise(*_exception)
  File "/usr/local/lib/python2.7/dist-packages/django/utils/autoreload.py", line 226, in wrapper
    fn(*args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/django/__init__.py", line 27, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/usr/local/lib/python2.7/dist-packages/django/apps/registry.py", line 85, in populate
    app_config = AppConfig.create(entry)
  File "/usr/local/lib/python2.7/dist-packages/django/apps/config.py", line 90, in create
    module = import_module(entry)
  File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
ImportError: No module named django_mongoengine
5
  • please provide an example of code you have tried and what you are looking to achieve. Commented Apr 6, 2017 at 12:32
  • may I ask why do you want to use mongodb instead of mysql or postgres for which django has good support? Commented Apr 6, 2017 at 13:13
  • django-mongoengine repo says the project is unstable. Commented Apr 6, 2017 at 13:16
  • @RaviKumar This project requires non-relational/nosql database... Commented Apr 6, 2017 at 13:24
  • Possible duplicate of How to use django together with mongoengine? Commented Apr 7, 2017 at 0:28

1 Answer 1

2

If you don't want to use the ORM then you can delete or comment out DATABASE = {} in settings.py.
A solution would be to create a connector that uses pymongo. Example of mongodb_connector.py:

from pymongo import MongoClient
from bson.json_util import dumps

class MongoConnector:
    username = 'user'
    password = 'pass'
    host = 'localhost'
    port = '27017'
    url = host + ':' + port
    client = MongoClient(mongo_url,username=username,password=password,authSource='admin',authMechanism='SCRAM-SHA-256')
    def find(self,db, collection, name):
        db = client[db]
        collection = db[collection]
        response = collection.find_one({"name":str(name)})
        return dumps(response)

Now you can get an object with:

from mongo_connector import MongoConnector
mongo = MongoConnector()
doc = mongo.find('mydb','mycollection','name')
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.