@@ -8,218 +8,7 @@ explore and build. The best way to share this is via our [MongoDB Community Foru
8
8
9
9
The development version of this package supports Django 5.0.x. To install it:
10
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:
20
-
21
- ``` bash
22
- $ django-admin startproject mysite --template https://github.com/mongodb-labs/django-mongodb-project/archive/refs/heads/5.0.x.zip
23
- ```
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
42
-
43
-
44
- class MongoAdminConfig (AdminConfig ):
45
- default_auto_field = " django_mongodb_backend.fields.ObjectIdAutoField"
46
-
47
-
48
- class MongoAuthConfig (AuthConfig ):
49
- default_auto_field = " django_mongodb_backend.fields.ObjectIdAutoField"
50
-
51
-
52
- class MongoContentTypesConfig (ContentTypesConfig ):
53
- default_auto_field = " django_mongodb_backend.fields.ObjectIdAutoField"
54
- ```
55
-
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
-
60
- ### Configuring migrations
61
-
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:
66
-
67
- ``` python
68
- MIGRATION_MODULES = {
69
- " admin" : " mongo_migrations.admin" ,
70
- " auth" : " mongo_migrations.auth" ,
71
- " contenttypes" : " mongo_migrations.contenttypes" ,
72
- }
73
- ```
74
-
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
-
99
- ``` bash
100
- $ python manage.py startapp myapp --template https://github.com/mongodb-labs/django-mongodb-app/archive/refs/heads/5.0.x.zip
101
- ```
102
- (where "5.0" matches the version of Django that you're using.)
103
-
104
- ### Configuring the ` DATABASES ` setting
105
-
106
- After you've set up a project, configure Django's ` DATABASES ` setting similar
107
- to this:
108
-
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 )
151
- ```
152
-
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
-
170
- * ` QuerySet.explain() ` supports the [ ` comment ` and ` verbosity ` options] (
171
- https://www.mongodb.com/docs/manual/reference/command/explain/#command-fields ).
172
-
173
- Example: ` QuerySet.explain(comment="...", verbosity="...") `
174
-
175
- Valid values for ` verbosity ` are ` "queryPlanner" ` (default),
176
- ` "executionStats" ` , and ` "allPlansExecution" ` .
177
-
178
- ## Known issues and limitations
179
-
180
- - The following ` QuerySet ` methods aren't supported:
181
- - ` bulk_update() `
182
- - ` dates() `
183
- - ` datetimes() `
184
- - ` distinct() `
185
- - ` extra() `
186
- - ` prefetch_related() `
187
-
188
- - ` QuerySet.delete() ` and ` update() ` do not support queries that span multiple
189
- collections.
190
-
191
- - ` DateTimeField ` doesn't support microsecond precision, and correspondingly,
192
- ` DurationField ` stores milliseconds rather than microseconds.
193
-
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 `
206
-
207
- - The ` tzinfo ` parameter of the ` Trunc ` database functions doesn't work
208
- properly because MongoDB converts the result back to UTC.
209
-
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.
220
-
221
- - Due to the lack of ability to introspect MongoDB collection schema,
222
- ` migrate --fake-initial ` isn't supported.
11
+ ` pip install django-mongodb-backend `
223
12
224
13
## Troubleshooting
225
14
0 commit comments