Skip to content

Commit 0484032

Browse files
committed
refactor
1 parent cec2076 commit 0484032

File tree

2 files changed

+13
-13
lines changed

2 files changed

+13
-13
lines changed

README.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,15 @@ $ python manage.py startapp myapp --template https://github.com/mongodb-labs/dja
103103

104104
### Configuring the `DATABASES` setting
105105

106-
After you've set up a project, configure Django's `DATABASES` setting similar
107-
to this:
106+
After you've set up a project, configure Django's `DATABASES` with
107+
`django_mongodb.parse(uri)`:
108+
109+
```python
110+
MONGODB_URI = "mongodb://<my_user>:<my_password>@localhost:27017/my_database"
111+
DATABASES["default"] = django_mongodb.parse(MONGODB_URI)
112+
```
113+
114+
Alternatively, you can configure the DATABASES settings directly:
108115

109116
```python
110117
DATABASES = {
@@ -121,13 +128,6 @@ DATABASES = {
121128
`OPTIONS` is an optional dictionary of parameters that will be passed to
122129
[`MongoClient`](https://pymongo.readthedocs.io/en/stable/api/pymongo/mongo_client.html).
123130

124-
Alternatively, you can use a MongoDB URI with `django_mongodb.parse(uri)`:
125-
126-
```python
127-
MONGODB_URI = "mongodb://<my_user>:<my_password>@localhost:27017/my_database"
128-
DATABASES["default"] = django_mongodb.parse(MONGODB_URI)
129-
```
130-
131131
Congratulations, your project is ready to go!
132132

133133
## Notes on Django QuerySets

django_mongodb/utils.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ def parse(uri, **kwargs):
3636
# If the fqdn is present, this is a SRV URI and the host is the fqdn.
3737
host = f"mongodb+srv://{uri['fqdn']}"
3838
else:
39-
# If the fqdn is not present, this is a standard URI and the host and
40-
# port are in the nodelist.
41-
nodelist = [f"{node[0]}:{node[1]}" for node in uri["nodelist"]]
42-
host, port = nodelist[0].split(":")
39+
if len(uri("nodelist")) == 1:
40+
host, port = uri("nodelist")[0].split(":")
41+
elif len(uri("nodelist")) > 1:
42+
host = ",".join(uri["nodelist"])
4343

4444
settings_dict = {
4545
"ENGINE": "django_mongodb",

0 commit comments

Comments
 (0)