django oracle inspectdb failure - django

i'm using django 1.3. i have an existing oracle database (10g) i would like to build Model's from using inspectdb.
'db': {
'ENGINE': 'django.db.backends.oracle',
'NAME': 'DBNAME',
'USER': 'me',
'PASSWORD': 'something',
}
so when run inspectdb i get:
$ python manage.py inspectdb --database db
cx_Oracle.InterfaceError: Unable to acquire Oracle environment handle
so i add
$ export ORACLE_HOME=/usr/oracle/
$ TWO_TASK=DBNAME
i try logging on with sqlplus with the same credentials and everything looks good.
so... i run inspectdb again, but this time i get
# This is an auto-generated Django model module.
# You'll have to do the following manually to clean this up:
# * Rearrange models' order
# * Make sure each model has one field with primary_key=True
# Feel free to rename the models, but don't rename db_table values or field names.
#
# Also note: You'll have to insert the output of 'django-admin.py sqlcustom [appname]'
# into your database.
from django.db import models
(ie it's blank)
any ideas? i had no problems getting this to work on a mysql database.

From the official docs.
inspectdb works with PostgreSQL, MySQL and SQLite. Foreign-key detection only works in PostgreSQL and with certain types of MySQL tables.
There is currently not a bug listed for it in the Django tracker if you wanted to submit it.

I have a similar setup at the top of my settings.py to set my environment variables for my oracle driver (Oracle 11.2). Not sure if this will help in your specific case.
### SETTING UP THE ENVIRONENT FOR OUR ORACLE RPM
import os
os.putenv('ORACLE_HOME', '/.../oracle/11.2')
os.putenv('LD_LIBRARY_PATH', '/.../oracle/11.2/lib')
I have had no issues with manage.py inspectdb (Django 1.2.7 and Django 1.4) on Oracle 11.2.

Related

How to integrate django with existing Postgres database

To use an existing database in Django, you need to have a model for each table. But creating models for existing tables manually is just too much work. However, there's no need to do that, since Django has a builtin tool to solve this exact problem.
Reference article linked here: how-to-integrate-django-with-existing-database
Edit settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': '<name>',
'USER': '<user>',
'PASSWORD': '<password>',
'HOST': '<host>',
'PORT': '<port>',
}
}
Generate models for linked existing database tables.
python manage.py inspectdb > models.py
Tweak your tables according to your preferences. Copy all tables and add them to your app models.py
Now create initial migrations for existing tables
python manage.py makemigrations
Run the migrate command to apply the migrations, Use --fake-initial option that applies the migrations where it's possible and skips the migrations where the tables are already there:
python manage.py migrate --fake-initial
At this point, any new changes to the model structure and subsequent migrations would work as if Django managed the database since its inception
Thanks to: Dima Knivets

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.

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.

Django: MySQL no such table: aidata.django_session

I'm running Django 1.4 on Windows 7 in Pycharm and I installed WAMP because I need to have my data in a MySQL table.
This is from setting.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'aidata',
'USER': 'root'
}
}
From installed_apps I uncommented:
'django.contrib.sessions'
Running manage.py syncdb does not create any tables ( even models) in my mysqldb.
I get the error when trying to acces /admin/
DatabaseError at /admin/
(1146, "Table 'aidata.django_session' doesn't exist")
Double check the db credentials
make sure you uncommented this line in your middleware:
MIDDLEWARE_CLASSES = (
....
'django.contrib.sessions.middleware.SessionMiddleware',
)
then try to python manage.py syncdb.
if you are still having issues post any output
EDIT -- NEXT CHECK:
do you have a "django_content_type" table?
if so, does that table have a "session" record?
if so, delete the session record and try to python manage.py syncdb
EDIT -- STEP 3:
now i'm guessing, post up your settings file so i can make meaningful troubleshooting attempts
Stop your server if you have one running
go into your file browser and delete the settings.pyc file
try to python manage.py syncdb
my thought is that a pyc file with the sqlLite info may be cached and not regenerating
EDIT -- STEP 4:
everything in your settings.py look ok to me. try something for me? create a new django project, don't enable the admin or add in your apps i just want to know if from scratch everything in your django install seems to be working
django-admin.py startproject testsite
do the database configuration/setup
python manage.py syncdb
let me know if the models create properly
I was running into the same problem and for me (running django 1.7 development trunk of mid-sept.2013) it helped to
remove all south migrations ([app]/migration)-directories
remove south from INSTALLED_APPS in settings.py
That might be due to the shift towards the integrated migration system in django v1.7, but I'm speculating here.

Django manage.py - Creating auth_permission and django_content_type tables

I am unable to use syncdb because my app uses some MySQL views. I have run manage.py sqlall <app>, but this does not output the SQL for django_content_type table or the auth_permission tables. I have also had a look into south and django evolution, but they both require syncdb, and I'm not sure they would help anyway.
I have manually added some models to the tables, but this is getting frustrating, and having installed the dbsettings app I am unsure of what I now need to enter.
Does anyone know of a way to get manage.py (or something else) to output the SQL for these tables and their contents?
Thanks.
Having done a bit more digging, I found these:
Fixing the auth_permission table after renaming a model in Django and manage.py sql command for django models - Django.
These output the tables, but not the data:
python manage.py sql auth
python manage.py sql admin
But this gets a lot closer. In the end I managed it with the following:
from django.contrib.auth.management import create_permissions
from django.db.models import get_apps
for app in get_apps():
create_permissions(app, None, 2)
from django.contrib.contenttypes.management import update_all_contenttypes
update_all_contenttypes(interactive=True)
This adds all the permissions and then all the content types which are needed. interactive=True means that it asks you if you want to remove stale content types.
#hajamie solution works for older supported version, taking a hint, below is what worked for me!
django = 1.9.7
from django.contrib.auth.management import create_permissions
from django.contrib.auth.models import Permission
from django.apps import apps
def fix_user_permission():
"""
run this method via shell whenever any amendments in any of the tables is made
"""
print "fixing user permissions"
# delete pre-existing user permission
Permission.objects.all().delete()
apps.models_module = True
create_permissions(apps, verbosity=0)
apps.models_module = None
print "process completed - fixed user permissions"
The easiest solution I found is to install Django Extensions, add it to settings.INSTALLED_APPS and run:
manage.py update_permissions