How to change the database local to be in a cloud (Django) - django

I wrote a project in django framework and I use the local DataBase SQLite, and I now want to upload it to the cloud so other people who work with me can access DataBase i.e. when I create a user then that user will exist with my co-workers.
Maybe someone has a guide on how to do this without modifying the DataBase so that I do not have to modify the rest of the code?

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.
go to monogodb site -> clickHere and create free account.
create new cluster
then you will reverse to this page if no in the left side press DataBase under DEPLOYMENT and then you get.
press on Connect buttom
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.

Related

Switch from SQLite to postgres, postgres asking for a password

Two things to note: One, I am executing these commands with my virtual environment running. So terminal appears as (env):. Two, every time I tried to enter su postgres, it asked me for a password.
I am reading a book on Django(using a mac), the text says:
"If you are using macOS, download PostgreSQL from https://www.postgresql.org/download/ and install it. You also need to install the Psycopg2 PostgreSQL adapter for Python. Run the following command to install it: pip install psycopg2==2.7.4."
I already had postgres installed on my machine, as I was previously fumbling with trying to deploy a website, before reverting back to this example project I had built. So I did not go to the website and install it. I installed psycopg2 as instructed ((env): pip install psycopg2==2.7.4.)
"Let's create a user for our PostgreSQL database. Open the shell and run the following commands: su postgres then createuser -dP blog"
I was thinking that "open the shell" meant enter the python virtual shell? So:
python manage.py shell
then
su postgres
this did not work, it asked me for a password. Exited the shell, ran the same command (env): su postgres, same result.
From there I thought maybe since I am in a virtual environment I do have to redownload postgres. I ran pip install postgres (did not know if this would download something, but it did, so I didn't bother going to the website/also would not know how to install it to this environment from the website). Note, this installed postgres as well as psycopg-someversion-binary. So now I have psycopg2==2.7.4 and some version of psycopg2-binary installed. From there I ran (env): su postgres (note from my environment). The same result, it asked for a password.
Then I reentered my python interactive shell with python manage.py shell and again ran su postgres. Same result.
At this point, I tried to research it on my own. What I read said I need to run su - postgres from root(?) by entering sudo -i at the terminal. I did this, now I am at Justins-MBP:~ root# (this does not seem it is at all related to the django project I am trying to use a postgres db for). Anyway, from here su - postgres and as you might have guessed, it asked me for a password.
Any help on how I can properly switch the db is greatly appreciated.
Also, after typing all of this and thinking about what I did I realized I haven't changed the database settings of the python project yet. But neither has the book, so not really sure how this project would even be aware of this database?
You probably have not yet set up the root user on MacOS, see this SO answer. However, for more information about the use of su, see this answer with a link to Apple's tech notes on how to create one.
The reason you should do this in your macOS terminal is because your setting up, configuring and doing database actions on a PostrgreSQL database, and this on its own, in the way your book is describing, is unrelated to Django (or Python for that matter).
So you have to make sure that 1) PostgreSQL is installed on your device and 2) that you have PostgreSQL running (as you will be creating users, which in fact means INSERTing data into your PostgreSQL Database). I used this blogpost (not affiliated) in the past to set up (install, run and configure) PostgreSQL on MacOS.
Regarding Django, you will need to change DATABASES in your settings.py (also see this part in Django tutorial)
DATABASES = {
# default namespace
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'dbname' #name of the database,
'USER': 'username' #name of the created user,
'PASSWORD': 'password',
'HOST': '127.0.0.1' #when local,
'PORT': '5432' #default psql port,
}
}

Django migrations: sqlite3 development db, Amazon Elastic Beanstalk and Amazon RDS postgresql live database

I'm wondering how the community would handle this particular scenario.
I have a Django app that I develop locally using an SQLite3 database as my development database.
The live application is hosted on Amazon Elastic Beanstalk and uses an Amazon RDS PostgreSQL database for production.
To deploy the app, I simply push the Django app to Elastic Beanstalk with eb deploy (which pushes the latest committed version from the local git repository).
settings.py configures the database and checks if the environment is live like so:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
if 'RDS_DB_NAME' in os.environ:
from settings_live import *
and settings_live.py changes the database configuration to the production settings like so:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': os.environ['RDS_DB_NAME'],
'USER': os.environ['RDS_USERNAME'],
'PASSWORD': CREDENTIALS['RDS_PASSWORD'],
'HOST': os.environ['RDS_HOSTNAME'],
'PORT': os.environ['RDS_PORT'],
}
}
This all works fine, but issues come up when it comes to migrations. For example: in my development environment I create a new model in an app's models.py. After making the change, I run manage.py makemigrations myapp and manage.py migrate. The migrations are properly applied to my sqlite3 development database. No problems.
Then I commit my changes in preparation for live deployment. My .gitignore file is configured to ignore db.sqlite3 as well as */migrations (since these migrations are only applicable to the development database).
Then I push my latest commit (which doesn't contain my dev database or associated migrations) to Elastic Beanstalk with eb deploy. I have configured an .ebextentions file (.ebextensions/02_commands.config) to run migrations on the production database like so:
03_makemigrations:
command: "django-admin.py makemigrations myapp1 myapp2"
leader_only: true
04_migrate:
command: "django-admin.py migrate"
leader_only: true
Here's the problem: any previous migrations that were generated in the Elastic Beanstalk environment with makemigrations no longer exist in app/migrations since the eb deploy deployment process overwrites the old app with the new one (which only contains a blank migrations directory). This leads to some unexpected behaviour such as tables not being created in the production database.
One solution I've considered (but haven't even begun to implement) is to create a script that copies migration files from an S3 bucket to */migrations and configure 02_commands.config to run this prior to running makemigrations and migrate. Then run another script afterwards that copies the new migrations files back to the S3 bucket. I just wonder if my whole workflow is wrong if it has come to this though.
Your mistake is in saying that the migrations are only applicable to the development database. That's just false. The whole point of migrations is that they are exactly intended to keep your development and production databases in sync. They are part of your code; they should be committed along with all the rest of the code, deployed to production, and run there.

Django unable to open database file using mysql

I'm a newbie in django so as python
I just succesfully configured my first django site over an apache server, then I configured it to work with mysql database editing the settings.py file and running the following command
python manage.py syncdb
I started playing a bit with the admin but occasionally when making get or post requests I get the following message OperationalError at "/some/route" unable to open database file
If I refresh the page loads fine, but if i keep refreshing any page in the admin the error shows up, so it's a random thing.
For what I've searched this is an issue related with sqlite, but I'm using mysql.
My database config in settings.py is the following:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'simulation',
'USER': 'root',
'PASSWORD': 'root',
'HOST': ''
}
}
I tried to specify hosts "localhost" and "127.0.0.1" but the result was the same.
It's really important for me find a solution that uses mysql as database engine.
Any help would be really appreciated! Thanks.
I've encountered a similar problem that you have right here, in my instance it seemed like my settings.py file was cached on the server. Once I deleted the .pyc files it started working fine.
Also, what happens if you change your database to sqlite? It might be worth checking that your mysql database isn't playing up instead of django.
I ran into the same problem with Django/Mysql/WSGI/Apache setup. Similar to Serakiel, I think that the problem stemmed from server caching. Restarting apache fixed the problem for me.

Setting up Websolr with Django on Heroku

I'm trying to add WebSolr support to my django powered app on Heroku. They have detailed instructions for an implementation using Ruby:
https://devcenter.heroku.com/articles/websolr
But I can't figure out how to tie it up to Haystack. Has any one done this?
Thanks.
Edit:
I was able to implement by modifying my settings.py file as:
HAYSTACK_URL = os.environ.get('WEBSOLR_URL', '')
HAYSTACK_CONNECTIONS = {
'default': {
'ENGINE': 'haystack.backends.solr_backend.SolrEngine',
'URL': HAYSTACK_URL,
},
}
Then running:
heroku run myproject/manage.py build_solr_schema > schema.xml
And uploading the contents of schema.xml to the advanced tab of the websolr interface. And once I ran
heroku run myproject/manage.py rebuild_index
the index was built.
I've updated the Heroku Websolr Docs to include a section on Django, based on your question. Thanks for that. For suggestions on our docs, drop a comment on this gist.

how to use manage.py syncdb outside of Django project, such as in Tornado?

I was looking through http://lincolnloop.com/blog/2009/sep/15/using-django-inside-tornado-web-server/ and I thought it was interesting and useful to use parts of Django if we need it in Tornado.
Based on the setup in http://lincolnloop.com/blog/2009/sep/15/using-django-inside-tornado-web-server/ how can we use manage.py syncdb ?
Here's what i have tried so far:
I've tried shifting manage.py to the same folder as the tornado project, and ran manage.py syncdb but it returns saying that settings.py is not found.
than i tried to move setting.py to the same folder and ran manage.py again. It tells me that no fixtures found. This time round, I have no idea how to configure settings.py since this is not a Django project.
Any advice or thoughts?
=================updates======================
Hi all,
continuing from the above an using advice provided by Agos,
i've tried running python manage.py syncdb --settings=dj_tornado and it returns
`"Error: Can't find the file 'settings.py'` in the directory containing 'manage.py'`. It appears you've customized things.
You'll have to run django-admin.py, passing it your settings module.
(If the file settings.py does indeed exist, it's causing an ImportError somehow.)"
So what i did is to run django-admin.py syncdb --settings=dj_tornado and it returns "django.core.exceptions.ImproperlyConfigured: You haven't set the database ENGINE setting yet."
But the weird thing is that the database engine has been set. How would I go about fixing this? i'm using django 1.2.3 and Tornado 0.2 by the way.
=================updates again======================
Hi all,
i've applied the advice provided by Agos, with a settings.py file in teh same folder as manage.py, and ran the command django-admin.py syncdb --settings=dj_tornado.
I still received the error:
django.core.exceptions.ImproperlyConfigured: You haven't set the database ENGINE setting yet.
But i have already configured the database based engine as follows:
in dj_tornado.py:
from django.conf import settings
settings.configure(
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': 'dev.db',
}
}
)
I'm kind of at my wits end. How do i use syncdb outside of Django project?
Best.
If I got it correctly, you can just use the --settings switch to point manage.py to the dj_tornado.py, which is your settings file after all
Update 1
from the help, available at python manage.py help:
Options:
--settings=SETTINGS The Python path to a settings module, e.g.
"myproject.settings.main". If this isn't provided, the
DJANGO_SETTINGS_MODULE environment variable will be
used.
So I would try this:
python manage.py syncdb --settings=dj_tornado
Update 2
Another error, another update to the answer!
First of all, consider that that blog post is quite old (september 2009). Django's DATABASES setting has been updated since 1.2.
The syntax in the blog post was:
settings.configure(DATABASE_ENGINE='sqlite3', DATABASE_NAME='dev.db')
With Django 1.2.X this is surely not correct. This would be the equivalent version:
settings.configure(DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': 'dev.db'
}
})
(sorry for the horrible formatting of the code).
If this still won't work, I'd consider creating a “standard” Django settings file to import. But my bet is on the db settings syntax.
Last update, I swear
Have you tried using django-admin.py again with the new syntax? If so, and still didn't work, a minimal settings.py would be just this:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': 'dev.db'
}
}
You can also keep the original configuration inside dj_tornado.py and use settings.py just to do syncdb.