InterviewSolution
| 1. |
How to connect MongoDB in Django? |
|
Answer» To leverage MongoDB from Django, the following SOFTWARE should be installed and running - Python 1. CREATE a PROJECT named 'testproj' with an application named 'testapp'. 2. Setup - Configure following in settings.py DATABASES = { 'default': { 'ENGINE': 'django_mongodb_engine', 'NAME': 'testdatabase', 'USER': '', 'PASSWORD': '', 'HOST': 'localhost', 'PORT': '27017', 'SUPPORTS_TRANSACTIONS': False, }, }3. Update models.py from django.db import models CLASS Article(models.Model): title = models.CharField(max_length = 64) CONTENT = models.TextField()4. Save - Save model data from a Django view. from django.http import HttpResponse from models import * def testview(request): article = Article(title = 'test title', content = 'test content' article.save() return HttpResponse("<h1>Saved!</h1>")5. Execute javascript query like from Django view to check connection- 6. Query MongoDB from Django to retrieve a list |
|