@@ -10,35 +10,30 @@ The development version of this package supports Django 5.0.x. To install it:
10
10
11
11
` pip install git+https://github.com/mongodb-labs/django-mongodb `
12
12
13
- Configure the Django ` DATABASES ` setting similar to this:
14
-
15
- ``` python
16
- DATABASES = {
17
- " default" : {
18
- " ENGINE" : " django_mongodb" ,
19
- " NAME" : " my_database" ,
20
- " USER" : " my_user" ,
21
- " PASSWORD" : " my_password" ,
22
- " OPTIONS" : {... },
23
- },
24
- }
25
- ```
26
-
27
- ` OPTIONS ` is an optional dictionary of parameters that will be passed to
28
- [ ` MongoClient ` ] ( https://pymongo.readthedocs.io/en/stable/api/pymongo/mongo_client.html ) .
13
+ ### Specifying the default primary key field
29
14
30
15
In your Django settings, you must specify that all models should use
31
16
` ObjectIdAutoField ` .
32
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/aclark4life/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
+
33
28
``` python
34
29
DEFAULT_AUTO_FIELD = " django_mongodb.fields.ObjectIdAutoField"
35
30
```
36
31
37
- This won't override any apps that have an ` AppConfig ` that specifies
38
- ` default_auto_field ` . For those apps, you'll need to create a custom
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
39
34
` AppConfig ` .
40
35
41
- For example, you might create ` mysite /apps.py` like this :
36
+ For example, the project template includes ` <project_name> /apps.py` :
42
37
43
38
``` python
44
39
from django.contrib.admin.apps import AdminConfig
@@ -58,14 +53,16 @@ class MongoContentTypesConfig(ContentTypesConfig):
58
53
default_auto_field = " django_mongodb.fields.ObjectIdAutoField"
59
54
```
60
55
61
- Then replace each app reference in the ` INSTALLED_APPS ` setting with the new
62
- `` AppConfig `` . For example, replace ` 'django.contrib.admin' ` with
63
- ` 'mysite.apps.MongoAdminConfig' ` .
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
64
61
65
62
Because all models must use ` ObjectIdAutoField ` , each third-party and contrib app
66
63
you use needs to have its own migrations specific to MongoDB.
67
64
68
- For example, you might configure your settings like this :
65
+ For example, ` settings.py ` in the project template specifies :
69
66
70
67
``` python
71
68
MIGRATION_MODULES = {
@@ -75,7 +72,9 @@ MIGRATION_MODULES = {
75
72
}
76
73
```
77
74
78
- After creating a ` mongo_migrations ` directory, you can then run:
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:
79
78
80
79
``` console
81
80
$ python manage.py makemigrations admin auth contenttypes
@@ -85,11 +84,44 @@ Migrations for 'admin':
85
84
...
86
85
```
87
86
88
- And whenever you run ` python manage.py startapp ` , you must remove the line:
87
+ ### Creating Django applications
88
+
89
+ Whenever you run ` python manage.py startapp ` , you must remove the line:
89
90
90
91
` default_auto_field = 'django.db.models.BigAutoField' `
91
92
92
- from the new application's ` apps.py ` file.
93
+ from the new application's ` apps.py ` file (or change it to reference
94
+ ` "django_mongodb.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/aclark4life/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" ,
113
+ " NAME" : " my_database" ,
114
+ " USER" : " my_user" ,
115
+ " PASSWORD" : " my_password" ,
116
+ " OPTIONS" : {... },
117
+ },
118
+ }
119
+ ```
120
+
121
+ ` OPTIONS ` is an optional dictionary of parameters that will be passed to
122
+ [ ` MongoClient ` ] ( https://pymongo.readthedocs.io/en/stable/api/pymongo/mongo_client.html ) .
123
+
124
+ Congratulations, your project is ready to go!
93
125
94
126
## Notes on Django QuerySets
95
127
0 commit comments