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

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/

Related

Heroku doesnt migrate models on Django

I just deploy my Django app to Heroku but I cant migrate my migrations to heroku. First I run :
heroku run python manage.py migrate all the migrations list as OK but when I showmigrations, none of them is migrating (all blank [ ]).
Then I try heroku run bash and migrate from there, everything seems ok even showmigrations from bash showing all of the migrations is working. I even manage to create a superuser. But when I open my admin page and log in with superuser it shows 'account.account' table does not exist and when I check showmigrations again all of the migrations are gone. I have been repeating this migration over and over and still can't figure this out.
Does anyone know what I am doing wrong here?
Edit :
I don't know if this is related but when I push my project to Heroku the first time, I'm using Postgre with this setting :
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': DB_NAME,
'USER': DB_USER,
'PASSWORD': DB_PASSWORD,
'HOST': 'localhost',
'PORT': '5432',
}
}
but when I try to run migrations it shows an error something like cant access localhost with 5432 port, it turns out that Heroku trying to access postgre in my localhost instead of using Heroku Postgres. The solution I found is to dump my db and reload it to Heroku. And since I don't know how to set that up. I just comment that postgre setting and replace it with default django setting :
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
}
}
create my dbsqlite file, running migrations again then once again push it to Heroku, hoping to find an easy way out but instead ended up in this problem
I'd recommend you to push a migrated project to heroku that is
python manage.py migrate
then push that migrated project to heroku
the reason your superuser doesn't get stored while creating superuser from heroku bash is because heroku has an ephemeral drive i.e heroku clears all the modified data from orignal push after equal interval of time.
It turns out that I forgot to install django-heroku. More on this : https://devcenter.heroku.com/articles/django-app-configuration

How to migrate the second db in django

I have two databases, one is default and the other one as first what I want is made some changes in first_db and now I want to migrate.
In my settings.py:
DATABASE_ROUTERS = ('app.router.DatabaseRouter',)
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'primary_name',
'USER': 'root',
'PASSWORD': 'password',
},
'first': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'secondary_name',
'USER': 'root',
'PASSWORD': 'password',
}
}
I tried the command python manage.py migrate --database=first it showed it got migrated then when i tried to do python manage.py makemigrations it said all migrations are done and when i tried to run server and trying to insert some value in some table of first it started giving me error like the column I added is not there can anyone help me.
I think the migration didn't happen actually so anyone can show me how correctly migrate when having multiple database.
Migrations are done in database named 'first'.
if you want to migrate them to default database too just in case, then run:
python3 manage.py migrate
otherwise run:
python3 manage.py migrate --fake #To fake migration in default db for resolving runtime error
And to insert some value into table:
ModelName.objects.using('first').create(id=1 .......(other_datas)
Reference

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

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!

Django to work with south requires MySQLdb

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.