|
1 | 1 | # Django MongoDB Backend
|
2 | 2 |
|
3 |
| -This backend is currently in development and is not advised for Production workflows. Backwards incompatible |
| 3 | +This backend is currently in development and is not advised for production workflows. Backwards incompatible |
4 | 4 | changes may be made without notice. We welcome your feedback as we continue to
|
5 |
| -explore and build. The best way to share this is via our [MongoDB Community Forum](https://www.mongodb.com/community/forums/tag/python) |
| 5 | +explore and build. The best way to share this is via our [MongoDB Community Forum](https://www.mongodb.com/community/forums/tag/python). |
6 | 6 |
|
7 |
| -## Install and usage |
8 |
| - |
9 |
| -The development version of this package supports Django 5.0.x. To install it: |
10 |
| - |
11 |
| -`pip install git+https://github.com/mongodb-labs/django-mongodb-backend` |
12 |
| - |
13 |
| -### Specifying the default primary key field |
14 |
| - |
15 |
| -In your Django settings, you must specify that all models should use |
16 |
| -`ObjectIdAutoField`. |
17 |
| - |
18 |
| -You can create a new project that's configured based on these steps using a |
19 |
| -project template: |
| 7 | +## Install |
20 | 8 |
|
| 9 | +Use the version of `django-mongodb-backend` that corresponds to your version of |
| 10 | +Django. For example, to get the latest compatible release for Django 5.0.x: |
21 | 11 | ```bash
|
22 |
| -$ django-admin startproject mysite --template https://github.com/mongodb-labs/django-mongodb-project/archive/refs/heads/5.0.x.zip |
| 12 | +$ pip install --pre django-mongodb-backend==5.0.* |
23 | 13 | ```
|
24 |
| -(where "5.0" matches the version of Django that you're using.) |
25 |
| - |
26 |
| -This template includes the following line in `settings.py`: |
27 |
| - |
28 |
| -```python |
29 |
| -DEFAULT_AUTO_FIELD = "django_mongodb_backend.fields.ObjectIdAutoField" |
30 |
| -``` |
31 |
| - |
32 |
| -But this setting won't override any apps that have an `AppConfig` that |
33 |
| -specifies `default_auto_field`. For those apps, you'll need to create a custom |
34 |
| -`AppConfig`. |
35 |
| - |
36 |
| -For example, the project template includes `<project_name>/apps.py`: |
37 |
| - |
38 |
| -```python |
39 |
| -from django.contrib.admin.apps import AdminConfig |
40 |
| -from django.contrib.auth.apps import AuthConfig |
41 |
| -from django.contrib.contenttypes.apps import ContentTypesConfig |
| 14 | +(Until the package is out of beta, you must use pip's `--pre` option.) |
42 | 15 |
|
43 | 16 |
|
44 |
| -class MongoAdminConfig(AdminConfig): |
45 |
| - default_auto_field = "django_mongodb_backend.fields.ObjectIdAutoField" |
| 17 | +## Quickstart |
46 | 18 |
|
| 19 | +### Start project |
47 | 20 |
|
48 |
| -class MongoAuthConfig(AuthConfig): |
49 |
| - default_auto_field = "django_mongodb_backend.fields.ObjectIdAutoField" |
| 21 | +From your shell, run the following command to create a new Django project |
| 22 | +called `example` using our custom template. Make sure the zipfile referenced |
| 23 | +at the end of the template link corresponds to your |
| 24 | +version of Django. The snippet below specifies `5.0.x.zip` at the end of |
| 25 | +the template url to get the template for any Django version matching 5.0: |
50 | 26 |
|
51 |
| - |
52 |
| -class MongoContentTypesConfig(ContentTypesConfig): |
53 |
| - default_auto_field = "django_mongodb_backend.fields.ObjectIdAutoField" |
| 27 | +```bash |
| 28 | +$ django-admin startproject example --template https://github.com/mongodb-labs/django-mongodb-project/archive/refs/heads/5.0.x.zip |
54 | 29 | ```
|
55 | 30 |
|
56 |
| -Each app reference in the `INSTALLED_APPS` setting must point to the |
57 |
| -corresponding ``AppConfig``. For example, instead of `'django.contrib.admin'`, |
58 |
| -the template uses `'<project_name>.apps.MongoAdminConfig'`. |
59 | 31 |
|
60 |
| -### Configuring migrations |
| 32 | +### Connect to the database |
61 | 33 |
|
62 |
| -Because all models must use `ObjectIdAutoField`, each third-party and contrib app |
63 |
| -you use needs to have its own migrations specific to MongoDB. |
64 |
| - |
65 |
| -For example, `settings.py` in the project template specifies: |
| 34 | +Navigate to your `example/settings.py` file and find the variable named |
| 35 | +`DATABASES` Replace the `DATABASES` variable with this: |
66 | 36 |
|
67 | 37 | ```python
|
68 |
| -MIGRATION_MODULES = { |
69 |
| - "admin": "mongo_migrations.admin", |
70 |
| - "auth": "mongo_migrations.auth", |
71 |
| - "contenttypes": "mongo_migrations.contenttypes", |
| 38 | +DATABASES = { |
| 39 | + "default": django_mongodb_backend.parse_uri("<CONNECTION_STRING_URI>"), |
72 | 40 | }
|
73 | 41 | ```
|
74 | 42 |
|
75 |
| -The project template includes these migrations, but you can generate them if |
76 |
| -you're setting things up manually or if you need to create migrations for |
77 |
| -third-party apps. For example: |
78 |
| - |
79 |
| -```console |
80 |
| -$ python manage.py makemigrations admin auth contenttypes |
81 |
| -Migrations for 'admin': |
82 |
| - mongo_migrations/admin/0001_initial.py |
83 |
| - - Create model LogEntry |
84 |
| -... |
85 |
| -``` |
86 |
| - |
87 |
| -### Creating Django applications |
88 |
| - |
89 |
| -Whenever you run `python manage.py startapp`, you must remove the line: |
90 |
| - |
91 |
| -`default_auto_field = 'django.db.models.BigAutoField'` |
92 |
| - |
93 |
| -from the new application's `apps.py` file (or change it to reference |
94 |
| - `"django_mongodb_backend.fields.ObjectIdAutoField"`). |
95 |
| - |
96 |
| -Alternatively, you can use the following `startapp` template which includes |
97 |
| -this change: |
98 |
| - |
| 43 | +The MongoDB `<CONNECTION_STRING_URI>` must also specify a database for the |
| 44 | +`parse_uri` function to work. |
| 45 | +If not already included, make sure you provide a value for `<DATABASE_NAME>` |
| 46 | +in your URI as shown in the example below: |
99 | 47 | ```bash
|
100 |
| -$ python manage.py startapp myapp --template https://github.com/mongodb-labs/django-mongodb-app/archive/refs/heads/5.0.x.zip |
| 48 | +mongodb+srv:// myDatabaseUser:D1fficultP%[email protected].mongodb .net/<DATABASE_NAME>?retryWrites=true&w=majority |
101 | 49 | ```
|
102 |
| -(where "5.0" matches the version of Django that you're using.) |
103 |
| - |
104 |
| -### Configuring the `DATABASES` setting |
105 | 50 |
|
106 |
| -After you've set up a project, configure Django's `DATABASES` setting similar |
107 |
| -to this: |
108 | 51 |
|
109 |
| -```python |
110 |
| -DATABASES = { |
111 |
| - "default": { |
112 |
| - "ENGINE": "django_mongodb_backend", |
113 |
| - "HOST": "mongodb+srv://cluster0.example.mongodb.net", |
114 |
| - "NAME": "my_database", |
115 |
| - "USER": "my_user", |
116 |
| - "PASSWORD": "my_password", |
117 |
| - "PORT": 27017, |
118 |
| - "OPTIONS": { |
119 |
| - # Example: |
120 |
| - "retryWrites": "true", |
121 |
| - "w": "majority", |
122 |
| - "tls": "false", |
123 |
| - }, |
124 |
| - }, |
125 |
| -} |
126 |
| -``` |
127 |
| - |
128 |
| -For a localhost configuration, you can omit `HOST` or specify |
129 |
| -`"HOST": "localhost"`. |
130 |
| - |
131 |
| -`HOST` only needs a scheme prefix for SRV connections (`mongodb+srv://`). A |
132 |
| -`mongodb://` prefix is never required. |
133 |
| - |
134 |
| -`OPTIONS` is an optional dictionary of parameters that will be passed to |
135 |
| -[`MongoClient`](https://pymongo.readthedocs.io/en/stable/api/pymongo/mongo_client.html). |
136 |
| - |
137 |
| -`USER`, `PASSWORD`, and `PORT` (if 27017) may also be optional. |
138 |
| - |
139 |
| -For a replica set or sharded cluster where you have multiple hosts, include |
140 |
| -all of them in `HOST`, e.g. |
141 |
| -`"mongodb://mongos0.example.com:27017,mongos1.example.com:27017"`. |
142 |
| - |
143 |
| -Alternatively, if you prefer to simply paste in a MongoDB URI rather than parse |
144 |
| -it into the format above, you can use: |
145 |
| - |
146 |
| -```python |
147 |
| -import django_mongodb_backend |
148 |
| - |
149 |
| -MONGODB_URI = "mongodb+srv://my_user:[email protected]/myDatabase?retryWrites=true&w=majority&tls=false" |
150 |
| -DATABASES["default"] = django_mongodb_backend.parse_uri(MONGODB_URI) |
| 52 | +### Run the server |
| 53 | +To verify that you installed Django MongoDB Backend and correctly configured your project, run the following command from your project root: |
| 54 | +```bash |
| 55 | +$ python manage.py runserver |
151 | 56 | ```
|
| 57 | +Then, visit http://127.0.0.1:8000/. This page displays a "Congratulations!" message and an image of a rocket. |
152 | 58 |
|
153 |
| -This constructs a `DATABASES` setting equivalent to the first example. |
154 |
| - |
155 |
| -#### `django_mongodb_backend.parse_uri(uri, conn_max_age=0, test=None)` |
156 |
| - |
157 |
| -`parse_uri()` provides a few options to customize the resulting `DATABASES` |
158 |
| -setting, but for maximum flexibility, construct `DATABASES` manually as |
159 |
| -described above. |
160 |
| - |
161 |
| -- Use `conn_max_age` to configure [persistent database connections]( |
162 |
| - https://docs.djangoproject.com/en/stable/ref/databases/#persistent-database-connections). |
163 |
| -- Use `test` to provide a dictionary of [settings for test databases]( |
164 |
| - https://docs.djangoproject.com/en/stable/ref/settings/#test). |
165 |
| - |
166 |
| -Congratulations, your project is ready to go! |
167 |
| - |
168 |
| -## Notes on Django QuerySets |
169 | 59 |
|
170 |
| -* `QuerySet.explain()` supports the [`comment` and `verbosity` options]( |
171 |
| - https://www.mongodb.com/docs/manual/reference/command/explain/#command-fields). |
| 60 | +## Capabilities of Django MongoDB Backend |
172 | 61 |
|
173 |
| - Example: `QuerySet.explain(comment="...", verbosity="...")` |
| 62 | +- **Model MongoDB Documents Through Django’s ORM** |
174 | 63 |
|
175 |
| - Valid values for `verbosity` are `"queryPlanner"` (default), |
176 |
| - `"executionStats"`, and `"allPlansExecution"`. |
| 64 | + - Store Django model instances as MongoDB documents. |
| 65 | + - Maps Django's built-in fields to MongoDB data types. |
| 66 | + - Provides custom fields for arrays (`ArrayField`) and embedded documents (`EmbeddedModelField`). |
| 67 | + - Supports core migration functionalities. |
| 68 | +- **Index Management** |
| 69 | + - Create single, compound, partial, and unique indexes using Django Indexes. |
| 70 | +- **Querying Data** |
| 71 | + - Supports most of the Django QuerySet API. |
| 72 | + - Supports relational field usage and executes `JOIN` operations with MQL. |
| 73 | + - A custom `QuerySet.raw_aggregate` method exposes MongoDB-specific operations like Vector Search, Atlas Search, and GeoSpatial querying to yield Django QuerySet results. |
| 74 | +- **Administrator Dashboard & Authentication** |
| 75 | + - Manage your data in Django’s admin site. |
| 76 | + - Fully integrated with Django's authentication framework. |
| 77 | + - Supports native user management features like creating users and session management. |
177 | 78 |
|
178 |
| -## Known issues and limitations |
179 | 79 |
|
180 |
| -- The following `QuerySet` methods aren't supported: |
181 |
| - - `bulk_update()` |
182 |
| - - `dates()` |
183 |
| - - `datetimes()` |
184 |
| - - `distinct()` |
185 |
| - - `extra()` |
186 |
| - - `prefetch_related()` |
| 80 | +### Issues & Help |
187 | 81 |
|
188 |
| -- `QuerySet.delete()` and `update()` do not support queries that span multiple |
189 |
| - collections. |
| 82 | +We're glad to have such a vibrant community of users of Django MongoDB Backend. We recommend seeking support for general questions through the [MongoDB Community Forums](https://www.mongodb.com/community/forums/tag/python). |
190 | 83 |
|
191 |
| -- `DateTimeField` doesn't support microsecond precision, and correspondingly, |
192 |
| - `DurationField` stores milliseconds rather than microseconds. |
193 | 84 |
|
194 |
| -- The following database functions aren't supported: |
195 |
| - - `Chr` |
196 |
| - - `ExtractQuarter` |
197 |
| - - `MD5` |
198 |
| - - `Now` |
199 |
| - - `Ord` |
200 |
| - - `Pad` |
201 |
| - - `Repeat` |
202 |
| - - `Reverse` |
203 |
| - - `Right` |
204 |
| - - `SHA1`, `SHA224`, `SHA256`, `SHA384`, `SHA512` |
205 |
| - - `Sign` |
| 85 | +#### Bugs / Feature Requests |
| 86 | +To report a bug or to request a new feature in Django MongoDB Backend, please open an issue in JIRA, our issue-management tool, using the following steps: |
206 | 87 |
|
207 |
| -- The `tzinfo` parameter of the `Trunc` database functions doesn't work |
208 |
| - properly because MongoDB converts the result back to UTC. |
| 88 | +1. [Create a JIRA account.](https://jira.mongodb.org/) |
209 | 89 |
|
210 |
| -- When querying `JSONField`: |
211 |
| - - There is no way to distinguish between a JSON "null" (represented by |
212 |
| - `Value(None, JSONField())`) and a SQL null (queried using the `isnull` |
213 |
| - lookup). Both of these queries return both of these nulls. |
214 |
| - - Some queries with `Q` objects, e.g. `Q(value__foo="bar")`, don't work |
215 |
| - properly, particularly with `QuerySet.exclude()`. |
216 |
| - - Filtering for a `None` key, e.g. `QuerySet.filter(value__j=None)` |
217 |
| - incorrectly returns objects where the key doesn't exist. |
218 |
| - - You can study the skipped tests in `DatabaseFeatures.django_test_skips` for |
219 |
| - more details on known issues. |
| 90 | +2. Navigate to the [Python Integrations project](https://jira.mongodb.org/projects/INTPYTHON/). |
220 | 91 |
|
221 |
| -- Due to the lack of ability to introspect MongoDB collection schema, |
222 |
| - `migrate --fake-initial` isn't supported. |
| 92 | +3. Click **Create Issue**. Please provide as much information as possible about the issue and the steps to reproduce it. |
223 | 93 |
|
224 |
| -## Troubleshooting |
| 94 | +Bug reports in JIRA for the Django MongoDB Backend project can be viewed by everyone. |
225 | 95 |
|
226 |
| -### Debug logging |
227 |
| - |
228 |
| -To troubleshoot MongoDB connectivity issues, you can enable [PyMongo's logging]( |
229 |
| -https://pymongo.readthedocs.io/en/stable/examples/logging.html) using |
230 |
| -[Django's `LOGGING` setting](https://docs.djangoproject.com/en/stable/topics/logging/). |
231 |
| - |
232 |
| -This is a minimal `LOGGING` setting that enables PyMongo's `DEBUG` logging: |
233 |
| - |
234 |
| -```python |
235 |
| -LOGGING = { |
236 |
| - "version": 1, |
237 |
| - "disable_existing_loggers": False, |
238 |
| - "handlers": { |
239 |
| - "console": { |
240 |
| - "class": "logging.StreamHandler", |
241 |
| - }, |
242 |
| - }, |
243 |
| - "loggers": { |
244 |
| - "pymongo": { |
245 |
| - "handlers": ["console"], |
246 |
| - "level": "DEBUG", |
247 |
| - }, |
248 |
| - }, |
249 |
| -} |
250 |
| -``` |
| 96 | +If you identify a security vulnerability in the driver or in any other MongoDB project, please report it according to the instructions found in [Create a Vulnerability Report](https://www.mongodb.com/docs/manual/tutorial/create-a-vulnerability-report/). |
0 commit comments