How to connect django with mongodb using mongoengine?

350 views
Skip to first unread message

Sagar

unread,
Jul 17, 2018, 6:29:43 AM7/17/18
to MongoEngine Users
Hello,
I want to connect my django rest-api with mongodb using mongoengine. But I'm not able to do it. I found so many example for that on google. But none of them working with the version I'm running. Below are the version of my frameworks.
django restframework: 2.0.7
mongoengine: 0.15
mongodb: I have created mongodb on mlab.com and it's version is 3.4
If you have the sample code for connection then please share it.

Gang Li

unread,
Jul 17, 2018, 11:51:41 AM7/17/18
to mongoeng...@googlegroups.com

Django does not support MongoDb directly.  Without patch Django FormModel  and other classes, you can only use MongoDB without Django Form support. TO just get data out of MongoDb, this can get your start:

 

import mongoengine

conn = mongoengine.connect('mysite1', **{"host" : "127.0.0.1", "tz_aware": True})

 

from MyUser_Data import User

 

# get all users

users = User.objects()

 

Sent from Mail for Windows 10

 


From: mongoeng...@googlegroups.com <mongoeng...@googlegroups.com> on behalf of Sagar <sagar.w...@gmail.com>
Sent: Tuesday, July 17, 2018 6:29:43 AM
To: MongoEngine Users
Subject: How to connect django with mongodb using mongoengine?
 
--
You received this message because you are subscribed to the Google Groups "MongoEngine Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongoengine-us...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Sagar

unread,
Jul 18, 2018, 5:41:32 AM7/18/18
to MongoEngine Users
Hello,
Thanks for code. I tried that code but I have this error.

    conn = backend.DatabaseWrapper(db, alias)
AttributeError: module 'mongoengine.base' has no attribute 'DatabaseWrapper'

This is my code.

conn = mongoengine.connect('dbname', **{'host': '127.0.0.1', 'tz_aware': True,})

Do you know solution for this error?

Gang Li

unread,
Jul 20, 2018, 1:59:51 PM7/20/18
to mongoeng...@googlegroups.com

The mongodb connection can’t be used directly for Django database  settings

You have to use it in your own code.

Django can’t talk to monogodb without heavy source patch.

Don’t use mongodb as a Django DATABASES

 

Sent from Mail for Windows 10

 

Sent: Wednesday, July 18, 2018 5:41:31 AM
To: MongoEngine Users
Subject: Re: How to connect django with mongodb using mongoengine?
 
Reply all
Reply to author
Forward
0 new messages