Integrating MongoDB with django?

Viewed 209

so I have a MongoDB database with data I collected. I want to take this data and send it in as a rest api, however I can't seem to find any engine to run mongodb using django that was made recently and for the current version of django. Does anyone know of such a library or can direct me to a recent django project that integrates Mongo successfully because I can't find any.

Thanks in advance.

3 Answers

You can use

  1. pymongo
  2. django-rest-framework-mongoengine
  3. mongoengine

sample of code

from mongoengine import Document, StringField, URLField, EmbeddedDocumentListField
from mongoengine.errors import ValidationError
from forms import Form

class ModelData(Document):
  """Document representing a service."""
  name = StringField(required=True)
  category = StringField(required=True)
  provider = StringField(required=True)
  forms = EmbeddedDocumentListField(Form, required=True)
  meta = {
    'indexes': [
      {'fields': ('name', 'category', 'provider'), 'unique': True},
      'name',
      'category',
      'provider'
    ]
  }

Have a nice trip here

You can use Djongo engine for integrating Django and MYSQL.

Step1:

pip install djongo
pip install mongoengine

Step2: in settings.py replace DATABASES variable with the below code

DATABASES = {
'default': {
  'ENGINE': 'djongo',
  'NAME' : 'Collection NAME',
  'HOST' : 'localhost'
 }
}

Step3:

run python manage.py migrate
Related