Search code examples
djangomongodbdjongo

Use mongoDB with django 3.0


As I said in the title I want to integrate mongoDB next to my Postgres Database in a Django3.0 project.

I used to use djongo* but it seems it's not compatible with the latest version of Django.
What do you think is the best connector to use mongoDB in a Django project ?

*https://github.com/nesdis/djongo


Solution

  • Yes you are right django 3.0 is not compatible djongo. You can use mongoengine (pip install mongoengine) to connect mongodb with python. It doesn't integrate with the Django ORM (no models) but allows you to define documents to work with.

    Please use below code in your project settings.py file

    import mongoengine
    import pymongo
    MONGODB_HOST = 'mongodb://127.0.0.1:27017'
    mongoengine.connect(db='db_name', host=MONGODB_HOST, 
        read_preference=pymongo.ReadPreference.PRIMARY_PREFERRED)