Skip to content

Commit d222bd8

Browse files
committed
document how to use startproject/app templates
1 parent 3352f43 commit d222bd8

File tree

1 file changed

+58
-26
lines changed

1 file changed

+58
-26
lines changed

README.md

Lines changed: 58 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -10,35 +10,30 @@ The development version of this package supports Django 5.0.x. To install it:
1010

1111
`pip install git+https://github.com/mongodb-labs/django-mongodb`
1212

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
2914

3015
In your Django settings, you must specify that all models should use
3116
`ObjectIdAutoField`.
3217

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+
3328
```python
3429
DEFAULT_AUTO_FIELD = "django_mongodb.fields.ObjectIdAutoField"
3530
```
3631

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
3934
`AppConfig`.
4035

41-
For example, you might create `mysite/apps.py` like this:
36+
For example, the project template includes `<project_name>/apps.py`:
4237

4338
```python
4439
from django.contrib.admin.apps import AdminConfig
@@ -58,14 +53,16 @@ class MongoContentTypesConfig(ContentTypesConfig):
5853
default_auto_field = "django_mongodb.fields.ObjectIdAutoField"
5954
```
6055

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
6461

6562
Because all models must use `ObjectIdAutoField`, each third-party and contrib app
6663
you use needs to have its own migrations specific to MongoDB.
6764

68-
For example, you might configure your settings like this:
65+
For example, `settings.py` in the project template specifies:
6966

7067
```python
7168
MIGRATION_MODULES = {
@@ -75,7 +72,9 @@ MIGRATION_MODULES = {
7572
}
7673
```
7774

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:
7978

8079
```console
8180
$ python manage.py makemigrations admin auth contenttypes
@@ -85,11 +84,44 @@ Migrations for 'admin':
8584
...
8685
```
8786

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:
8990

9091
`default_auto_field = 'django.db.models.BigAutoField'`
9192

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!
93125

94126
## Notes on Django QuerySets
95127

0 commit comments

Comments
 (0)