如何在Django4.0.6中使用MongoDB数据库

35 views
Skip to first unread message

Hella thor

unread,
Jul 19, 2022, 10:47:26 PMJul 19
to Django users
Hi guys
我查询了很多的资料,发现Django无法很好的连接MongoDB(该数据库在其他的服务器中),我需要解决如下问题:

1、在settings.py文件中设置连接MongoDB,需要密码
2、在models.py文件中建立模型
3、在views.py中进行数据的读写操作

我尝试了在python中进行连接,但是我不知道如何在Django中进行连接MongoDB,以下是我的环境配置信息:

python==3.9.0
django==4.0.6
pymongo==4.1.1
mongoengine==0.24.1

DATABASE
0220720104558.png

如果您帮我,我将对您万分感谢。

Juergen Hauser

unread,
Jul 31, 2022, 6:23:31 PMJul 31
to Django users
pip install djongo
pip install pytz
pip install pymongo==3.12.3

settings.py:

import djongo

#Database is localhost:
DATABASES = {
'default': {
'ENGINE': 'djongo',
'NAME': 'your-db-name',
'ENFORCE_SCHEMA': False
}
}

#Database is server:
DATABASES = {
 'default': {
 'ENGINE': 'djongo',
 'NAME': 'your-db-name',
'ENFORCE_SCHEMA': False,
 'CLIENT': {
'host': 'host-name or ip address',
'port': port_number, 'username': 'db-username',
 'password': 'password',
'authSource': 'db-name',
 'authMechanism': 'SCRAM-SHA-1'
 },
'LOGGING': {
'version': 1,
'loggers': {
 'djongo': {
'level': 'DEBUG',
 'propagate': False,
 }
 },
},
 }
}


Django Terminal:

python  manage.py makemigrations

python  manage.py migrate

Satyajit Barik

unread,
Aug 1, 2022, 10:26:53 AMAug 1
to django...@googlegroups.com

I am delighted to invite you on behalf of python & django to become a free member today in WhatsApp group.


Here you can join in our WhatsApp group using below link👇


https://chat.whatsapp.com/Bq2jcxLJVG50v9Wbkvz6Vy


[Membership benefits] Here are some great perks you get for becoming a member:

  • Ask your python & django related problems: get quick possible answer.
  • Friendly environment: you can learn python & django from core.
  • Daily Python Assignment exercise.


Thank you for your consideration, I look forward to hearing from you and hope to see you at our WhatsApp python group.





--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/36c0e6d2-93aa-4a10-86ba-1de35a9b5e43n%40googlegroups.com.

Hella thor

unread,
Aug 1, 2022, 11:40:49 PMAug 1
to django...@googlegroups.com
Thank you Buddy.
I've taken care of it.
Have a nice day.
Thank you aggin.

Juergen Hauser <jogib...@gmail.com> 于2022年8月1日周一 06:23写道:
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.

Hella thor

unread,
Aug 1, 2022, 11:44:47 PMAug 1
to django...@googlegroups.com
Thank you.  I'm already on board.

Satyajit Barik <satyajit...@gmail.com> 于2022年8月1日周一 22:26写道:
Reply all
Reply to author
Forward
0 new messages