@@ -4,176 +4,8 @@ This backend is currently in development and is not advised for Production workf
4
4
changes may be made without notice. We welcome your feedback as we continue to
5
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:
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" ` .
7
+ The documentation in the "docs" directory is online at
8
+ https://django-mongodb-backend.readthedocs.io/en/latest/ .
177
9
178
10
## Known issues and limitations
179
11
@@ -220,31 +52,3 @@ Congratulations, your project is ready to go!
220
52
221
53
- Due to the lack of ability to introspect MongoDB collection schema,
222
54
` migrate --fake-initial ` isn't supported.
223
-
224
- ## Troubleshooting
225
-
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
- ```
0 commit comments