A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.mongodb.com/docs/languages/python/django-mongodb/current/connect/ below:

Configure Your Database Connection - Django MongoDB Backend v5.2

In this guide, you can learn how to configure your Django project's connection to MongoDB.

After installing Django MongoDB Backend and creating a project, you can configure your connection to MongoDB in the following ways:

To manually configure your connection to MongoDB, update the DATABASES setting in your project's settings.py file. Set it to a dictionary containing the default key:

DATABASES = {    "default": {            },}

To configure the default key, assign a nested dictionary as its value. This nested dictionary has the following keys:

Key

Description

ENGINE

The backend driver to use for the connection. Set this key to "django_mongodb_backend".

HOST

Your connection URI. For localhost connections, this key is optional.

For SRV connections, you must include a scheme prefix (mongodb+srv://).

To specify more than one host, include all hostnames in one string. Use a comma to separate each hostname.

Example: "HOST": "mongodb://mongos0.example.com:27017,mongos1.example.com:27017"

NAME

The database you want to use.

USER

The username for authenticating to the database, if your connection requires authentication.

PASSWORD

The password for your database user, if your connection requires authentication.

PORT

The port number on which the database server is listening. The default port is 27017.

For MongoDB Atlas connections, this key is optional.

OPTIONS

A dictionary of additional connection options for the database. This key is optional.

To see a full list of connection options that you can set in the

OPTIONS

key, see the optional parameters for

MongoClient

in the PyMongo API documentation.

In this example, the DATABASES setting performs the following actions:

DATABASES = {    "default": {        "ENGINE": "django_mongodb_backend",        "HOST": "mongodb+srv://cluster0.example.mongodb.net",        "NAME": "my_database",        "USER": "my_user",        "PASSWORD": "my_password",        "PORT": 27017,        "OPTIONS": {            "retryWrites": "true",            "w": "majority",        },    },}

To automatically construct the DATABASES setting that configures your MongoDB connection, you can use the parse_uri() function. This function accepts the following arguments:

The following example uses the parse_uri() function to specify the same connection configuration as the previous manual configuration example:

import django_mongodb_backendMONGODB_URI = "mongodb+srv://my_user:my_password@cluster0.example.mongodb.net/?retryWrites=true&w=majority"DATABASES["default"] = django_mongodb_backend.parse_uri(MONGODB_URI, db_name="<database name>")

To view a sample project that configures a MongoDB database connection, see the Configure your MongoDB Connection step in the Getting Started tutorial.

To learn more about Django settings, see Settings in the Django documentation.


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4