[Answered ]-How to change the database local to be in a cloud (Django)

1👍

Ok i found the answer,
what you should do is Connect Django and MongoDB Using Djongo

you can read from here

I RECOMMEND TO FOLLOW MY SETPS.

  1. go to monogodb site -> clickHere and create free account.
  2. create new cluster
  3. then you will reverse to this page if no in the left side press DataBase under DEPLOYMENT and then you get. enter image description here
  4. press on Connect buttom enter image description here
  5. choose "Connect your application" and then choose python and the last version

after all this you will have this link:
for example!!!

mongodb+srv://<username>:<password>@<atlas cluster>/<myFirstDatabase>?retryWrites=true&w=majority

copy this link and do the next setps:

  • go back to your terminal and install this package: pip install djongo
  • and open settings.py
  • change your DATEBASE to like this:
DATABASES = {
        'default': {
            'ENGINE': 'djongo',
            'NAME': 'your-db-name',
            'CLIENT': {
                'host': 'mongodb+srv://<username>:<password>@<atlascluster>/<myFirstDatabase>?retryWrites=true&w=majority'
            }  
        }
}
  • in ‘host’ paste the link you copy from monogodb site.
  • in ‘NAME’ write your name of the database.

Now that we have the Django project (and app), you can create the collections in MongoDB using the commands:

python manage.py makemigrations <app-name>

python manage.py migrate

if you get error like ‘django.db.utils.databaseerror’
what you should to do is:
It deletes all the files that are in the migrations folder that are in the apps you have created and after that run again the command above.

👤OmerBY

Leave a comment