|
| 1 | +=================================================== |
| 2 | +Configuring a project to use Django MongoDB Backend |
| 3 | +=================================================== |
| 4 | + |
| 5 | +Aftering :doc:`installing Django MongoDB Backend <install>`, you must take some |
| 6 | +additional steps to configure your project. |
| 7 | + |
| 8 | +.. _specifying the-default-pk-field: |
| 9 | + |
| 10 | +Specifying the default primary key field |
| 11 | +======================================== |
| 12 | + |
| 13 | +In your Django settings, you must specify that all models should use |
| 14 | +:class:`~django_mongodb_backend.fields.ObjectIdAutoField`. |
| 15 | + |
| 16 | +You can create a new project that's configured based on these steps using a |
| 17 | +project template: |
| 18 | + |
| 19 | +.. code-block:: bash |
| 20 | +
|
| 21 | + $ django-admin startproject mysite --template https://github.com/mongodb-labs/django-mongodb-project/archive/refs/heads/5.0.x.zip |
| 22 | +
|
| 23 | +(If you're using a version of Django other than 5.0.x, replace the two numbers |
| 24 | +to match the first two numbers from your version.) |
| 25 | + |
| 26 | +This template includes the following line in ``settings.py``:: |
| 27 | + |
| 28 | + DEFAULT_AUTO_FIELD = "django_mongodb_backend.fields.ObjectIdAutoField" |
| 29 | + |
| 30 | +But this setting won't override any apps that have an ``AppConfig`` that |
| 31 | +specifies :attr:`~django.apps.AppConfig.default_auto_field`. For those apps, |
| 32 | +you'll need to create a custom :class:`~django.apps.AppConfig`. |
| 33 | + |
| 34 | +For example, the project template includes ``<project_name>/apps.py``:: |
| 35 | + |
| 36 | + from django.contrib.admin.apps import AdminConfig |
| 37 | + from django.contrib.auth.apps import AuthConfig |
| 38 | + from django.contrib.contenttypes.apps import ContentTypesConfig |
| 39 | + |
| 40 | + |
| 41 | + class MongoAdminConfig(AdminConfig): |
| 42 | + default_auto_field = "django_mongodb_backend.fields.ObjectIdAutoField" |
| 43 | + |
| 44 | + |
| 45 | + class MongoAuthConfig(AuthConfig): |
| 46 | + default_auto_field = "django_mongodb_backend.fields.ObjectIdAutoField" |
| 47 | + |
| 48 | + |
| 49 | + class MongoContentTypesConfig(ContentTypesConfig): |
| 50 | + default_auto_field = "django_mongodb_backend.fields.ObjectIdAutoField" |
| 51 | + |
| 52 | +Each app reference in the :setting:`INSTALLED_APPS` setting must point to the |
| 53 | +corresponding ``AppConfig``. For example, instead of ``'django.contrib.admin'``, |
| 54 | +the template uses ``'<project_name>.apps.MongoAdminConfig'``. |
| 55 | + |
| 56 | +Configuring migrations |
| 57 | +====================== |
| 58 | + |
| 59 | +Because all models must use |
| 60 | +:class:`~django_mongodb_backend.fields.ObjectIdAutoField`, each third-party |
| 61 | +and contrib app you use needs to have its own migrations specific to MongoDB. |
| 62 | + |
| 63 | +For example, ``settings.py`` in the project template specifies:: |
| 64 | + |
| 65 | + MIGRATION_MODULES = { |
| 66 | + "admin": "mongo_migrations.admin", |
| 67 | + "auth": "mongo_migrations.auth", |
| 68 | + "contenttypes": "mongo_migrations.contenttypes", |
| 69 | + } |
| 70 | + |
| 71 | +The project template includes these migrations, but you can generate them if |
| 72 | +you're setting things up manually or if you need to create migrations for |
| 73 | +third-party apps. For example: |
| 74 | + |
| 75 | +.. code-block:: bash |
| 76 | +
|
| 77 | + $ python manage.py makemigrations admin auth contenttypes |
| 78 | + Migrations for 'admin': |
| 79 | + mongo_migrations/admin/0001_initial.py |
| 80 | + - Create model LogEntry |
| 81 | + ... |
| 82 | +
|
| 83 | +Creating Django applications |
| 84 | +============================ |
| 85 | + |
| 86 | +Whenever you run ``python manage.py startapp``, you must remove the line:: |
| 87 | + |
| 88 | + default_auto_field = 'django.db.models.BigAutoField' |
| 89 | + |
| 90 | +from the new application's ``apps.py`` file (or change it to reference |
| 91 | +``"django_mongodb_backend.fields.ObjectIdAutoField"``). |
| 92 | + |
| 93 | +Alternatively, you can use the following :djadmin:`startapp` template which |
| 94 | +includes this change: |
| 95 | + |
| 96 | +.. code-block:: bash |
| 97 | +
|
| 98 | + $ python manage.py startapp myapp --template https://github.com/mongodb-labs/django-mongodb-app/archive/refs/heads/5.0.x.zip |
| 99 | +
|
| 100 | +(If you're using a version of Django other than 5.0.x, replace the two numbers |
| 101 | +to match the first two numbers from your version.) |
| 102 | + |
| 103 | +.. _configuring-databases-setting: |
| 104 | + |
| 105 | +Configuring the ``DATABASES`` setting |
| 106 | +===================================== |
| 107 | + |
| 108 | +After you've set up a project, configure Django's :setting:`DATABASES` setting |
| 109 | +similar to this:: |
| 110 | + |
| 111 | + DATABASES = { |
| 112 | + "default": { |
| 113 | + "ENGINE": "django_mongodb_backend", |
| 114 | + "HOST": "mongodb+srv://cluster0.example.mongodb.net", |
| 115 | + "NAME": "my_database", |
| 116 | + "USER": "my_user", |
| 117 | + "PASSWORD": "my_password", |
| 118 | + "PORT": 27017, |
| 119 | + "OPTIONS": { |
| 120 | + # Example: |
| 121 | + "retryWrites": "true", |
| 122 | + "w": "majority", |
| 123 | + "tls": "false", |
| 124 | + }, |
| 125 | + }, |
| 126 | + } |
| 127 | + |
| 128 | +For a localhost configuration, you can omit :setting:`HOST` or specify |
| 129 | +``"HOST": "localhost"``. |
| 130 | + |
| 131 | +:setting:`HOST` only needs a scheme prefix for SRV connections |
| 132 | +(``mongodb+srv://``). A ``mongodb://`` prefix is never required. |
| 133 | + |
| 134 | +:setting:`OPTIONS` is an optional dictionary of parameters that will be passed |
| 135 | +to :class:`~pymongo.mongo_client.MongoClient`. |
| 136 | + |
| 137 | +Specify :setting:`USER` and :setting:`PASSWORD` if your database requires |
| 138 | +authentication. |
| 139 | + |
| 140 | +:setting:`PORT` is optional if unchanged from MongoDB's default of 27017. |
| 141 | + |
| 142 | +For a replica set or sharded cluster where you have multiple hosts, include |
| 143 | +all of them in :setting:`HOST`, e.g. |
| 144 | +``"mongodb://mongos0.example.com:27017,mongos1.example.com:27017"``. |
| 145 | + |
| 146 | +Alternatively, if you prefer to simply paste in a MongoDB URI rather than parse |
| 147 | +it into the format above, you can use |
| 148 | +:func:`~django_mongodb_backend.utils.parse_uri`:: |
| 149 | + |
| 150 | + import django_mongodb_backend |
| 151 | + |
| 152 | + MONGODB_URI = "mongodb+srv://my_user:[email protected]/myDatabase?retryWrites=true&w=majority&tls=false" |
| 153 | + DATABASES["default"] = django_mongodb_backend.parse_uri(MONGODB_URI) |
| 154 | + |
| 155 | +This constructs a :setting:`DATABASES` setting equivalent to the first example. |
| 156 | + |
| 157 | +Congratulations, your project is ready to go! |
0 commit comments