Django to work with south requires MySQLdb - django

I am following the django instruction to learn django in eclipse.
I came to the part of running cmd
python manage.py migrate
and it complains about unknown command migrate.
Googled. Knew that it requires South module to be included. I downloaded/installed south, and added 'south' in the INSTALLED_APPS.
I ran the command again, this time it complains
import MySQLdb as Database
ImportError: No module named 'MySQLdb'
So I looked for MySQLdb, only to find that there is none for python 3.
I could not find anything useful. So what do you do to make django to work with mysql?
I know there're other connectors around, but I am trying to follow the django tutorial and it seems that 'migrate' cmd must use 'south' and 'south' must use MySQLdb(?)
--- update ---
Here is the DB settings in settings.py:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'test',
'USER': 'root',
'PASSWORD': '******',
'HOST': '127.0.0.1',
'PORT': '3306',
}
}
I suspect 'ENGINE' has to be something else, but I failed to find enough information online to figure it out...

You can switch to any database you want MySQL or postgresql or sqlite etc for your django app. South uses the default database engine from your django setting DATABASES. As stated here
South automatically exposes the correct set of database API operations
as south.db.db; it detects which database backend you’re using from
your Django settings file.

Related

django-admin dbshell raises django.core.exceptions.ImproperlyConfigured

I know this question has been asked before, but none of them worked for me so far, so I'm going to give it a chance here.
I'm trying to use MySQL as my database in django, but when I modify the settings.py and run the command:
django-admin dbshell
I get the following error:
django.core.exceptions.ImproperlyConfigured: Requested setting DATABASES,
but settings are not configured. You must either define the environment
variable DJANGO_SETTINGS_MODULE or call settings.configure()
before accessing settings.
What I did:
- I'm running windows 10.
using pipenv, I create fresh virtual environment.
install django.
start new project.
edit the settings.py
in the settings.py I change the DATABASES to the following:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'test_db',
'USER': 'root',
'PASSWORD': '****',
'HOST': '127.0.0.1',
'PORT': '3306',
}
}
also pip installed mysqlclient
Weird thing is that when I make migrations, new tables are created, which means that DB works. why doesn't the command
django-admin dbshell
work?
based on django docs, manage.py does same thing as django-admin, and also adds settings to the sys path, therefore manage.py should be used instead of django-admin
https://docs.djangoproject.com/en/2.1/ref/django-admin/

Is there a way to mention the database settings while starting the Django app?

From the docs, it looks like the database needs to be present in the settings.py. Is there a way to pass the database details while running python manage.py runserver given the migrations have been completed?
Edit -
Use case - The database is not known beforehand so I can't have it hardcoded in settings.py. Also, there will be one database for the entire app.
Using a settings file other than settings.py in Django - This questions answers how to use a settings file different than settings.py. However, I am looking for ways in which I don't use settings file to specify the database credentials.
split your settings.py into two files
base_settings.py (add all the common settings here)
development.py
(add your database settings here)
like :
from .base_settings import *
DATABASES = {
'default': {
'ENGINE': 'XX.db.backends.postgresql',
'NAME': 'XX',
'USER': 'postgres',
'ATOMIC_REQUESTS':True,
'PASSWORD': '*****',
'HOST': '0.0.0.0',
'PORT': '5432',
}
}
and run your project using
python manage.py runserver 0.0.0.0:8002 --settings=django_project_name.development

setting database: postgresql in django 1.8 in heroku

I have been struggling for this issue for the whole days while no solutions at all. so I post it here.
I am trying to set up a blog website in Heroku via Django 1.8 which uses Python 3.4.3. I follows the instructions from Heroku website here.
I use "foreman start" to run Django project in my Mac and I already installed all dependence.
Part of my setting.py file involving the database initially looks like:
import dj_database_url
DATABASES = {}
DATABASES['default'] = dj_database_url.config()
Then I got error: ImproperlyConfigured at /settings.DATABASES is improperly configured. Please supply the ENGINE value.
Then I modify the files by adding one line supplying the ENGINE value:
import dj_database_url
DATABASES = {}
DATABASES['default'] = dj_database_url.config()
DATABASES['default']['ENGINE'] = 'django.db.backends.postgresql_psycopg2'
Based on this post answered by Or Arbel, it should work. But I got another error: ImproperlyConfigured at /settings.DATABASES is improperly configured. Please supply the NAME value.
What should I do next? Actually my Django project is very simple and does not involve any database operations(may need in the future). I just want to make it works on Heroku. Thanks!
Do I need to create a database to continue? I just want to make the webpage works.
Thanks for your guys help, specially souldeux.
Update:
I have fixed the issue by using souldeux's method by providing more informations about the database. Here I want to emphasis that it seems the code from the original Heroku tutorial does not work for Django 1.8:
import dj_database_url ####not working for my case
DATABASES = {}
DATABASES['default'] = dj_database_url.config()
Initially I did not create a database because I think it is not necessary for simple projects, based on my understanding obtained from Heroku tutorial. Actually it does need to create a database in Heroku to make it works. The tutorial is here. You need run "heroku config -s | grep HEROKU_POSTGRESQL" to get the database information. The format is like:
scheme://username:password#host:port/database
So you can get 'database', 'username', 'password', etc.
Afterwards, modify the 'settings.py' according to souldeux, then run following codes:
git add .
git commit -m "Ready to go to Heroku"
git push heroku master
heroku run python manage.py syncdb
Now it works. But other issues arise like my webpages do not show images... Anyway it solved. Please confirm my solutions, thanks.
I think you need to add more information to your database definition. For instance, here's what my own database entry looks like in my settings.py:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'django',
'USER': 'redacted',
'PASSWORD': 'redacted',
'HOST': '127.0.0.1',
'PORT': '5432',
}
}
If you don't have a database user and password to enter in the fields marked redacted then you need to make sure you have the actual database created and psycopg2 installed.

Mezzanine: where is the database created by `manage.py createdb`

I have the following database settings in Mezzanine:
DATABASES = {
"default": {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'lucidDB',
'USER': 'lucid',
'PASSWORD': 'xxxxxxxx',
'HOST': 'localhost',
'PORT': '',
}
}
I run the command python manage.py createdb and then answer yes to the question Would you like to fake initial migrations? (yes/no):.
Note: south was installed.
My questions:
I checked in postgres (postgres# psql --> postgres=# \l), but didn't find the database lucidDB. However, the system runs OK. Where exactly is the database created?
What does it mean by fake migration?
I didn't run syncdb or makemigrations --> migrate yet, why the system worked?
You didn't find the LucidDB in postgres because it wasn't created, you need to create it with the postgres shell see here for detailed description.
Faking a migration is marking it as complete without actually changing the database schema, it will simply add an entry in the migrationhistory database, see detailed description here.
On the Mezzanine docs it is stated that createdb is a shortcut for syncdb and migrate commands, see here for detailed description.
Hope this helps!

Facing issue with South and Django "There is no South database module 'south.db.postgresql_psycopg2' for your database."

I am try to develop one project in Python and Django using South for database migration.
I am very new to south and currently facing one problem when try to run any command syncdb or runserver that is :
"There is no South database module 'south.db.postgresql_psycopg2' for your database. Please either choose a supported database, check for SOUTH_DATABASE_ADAPTER[S] settings, or remove South from INSTALLED_APPS."
I am using following versions
Django==1.4.2,
South==0.7.6,
psycopg2==2.4.5
Postgres 9.2.2
Python==2.7.1 for the configuration.
I am trying to configure this project on mac machine 10.7.5
Database Settings info:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'xxx',
'USER': 'xxx',
'PASSWORD': 'xxxx',
'HOST': '',
'PORT': '',
}
}
SOUTH_DATABASE_ADAPTERS = {
'default': 'south.db.postgresql_psycopg2',
}
Thanks in Advance
Regards,
AnshJ
Its very sill mistake by me, I have 2 different version of south in my project path and one of south didn't have postgresql_psycopg2.py file under db folder and which actually get loaded.
Thanks,
Regards,
AnshJ