0

So I am building a Django site that is connecting to a view legacy DBs. Because of some of the limitations of the legacy MySQL dbs I need to access a view. From what I can tell the easiest way is to insert sql directly. (inspectdb doesn't work for the views).

Here is the code I have so far:

from django.db import connection

cursor = connection.cursor()

sqlstring = 'SELECT * FROM Site_Call_Counts'

cursor.execute(sqlstring)
result = cursor.fetchall()

This code works when I enter it into the manage.py shell but breaks when I try and run it complete. Here is the traceback.

/usr/bin/python2.7 /root/Desktop/IPNV/django-test/src/djangotest/viewTest.py
Traceback (most recent call last):
  File "/root/Desktop/IPNV/django-test/src/djangotest/viewTest.py", line 3, in <module>
    cursor = connection.cursor()
  File "/usr/local/lib/python2.7/dist-packages/django/db/__init__.py", line 33, in __getattr__
    return getattr(connections[DEFAULT_DB_ALIAS], item)
  File "/usr/local/lib/python2.7/dist-packages/django/db/utils.py", line 208, in __getitem__
    self.ensure_defaults(alias)
  File "/usr/local/lib/python2.7/dist-packages/django/db/utils.py", line 176, in ensure_defaults
    conn = self.databases[alias]
  File "/usr/local/lib/python2.7/dist-packages/django/utils/functional.py", line 35, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/usr/local/lib/python2.7/dist-packages/django/db/utils.py", line 156, in databases
    self._databases = settings.DATABASES
  File "/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py", line 56, in __getattr__
    self._setup(name)
  File "/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py", line 39, in _setup
    % (desc, ENVIRONMENT_VARIABLE))
django.core.exceptions.ImproperlyConfigured: Requested setting DATABASES, but settings are not configured.

So really I have 2 Questions. First, is this the easiest way to accomplish what I need, and 2nd, if it is how to I fix this error. I have looked at the docs and the posts here and here. The docs are django docs

This may be obvious but I am newer to coding so be gentle lol.

EDIT*** Here is my db definition in settings.py (same directory)

 },
    'default': {
        'NAME': 'app2',
        'ENGINE': 'django.db.backends.mysql',
        'USER': 'admin',
        'PASSWORD': 'password',
        'HOST': '10.1.1.1',
        'PORT': '3306',

    },
Joe
  • 2,641
  • 5
  • 22
  • 43
  • I think you should first run through the [Django tutorial](https://docs.djangoproject.com/en/1.11/intro/tutorial01/) to see how to set up a new Django project and run it. At the moment you are just trying to execute a single Python file, not run a Django project (along with its configuration) – Timmy O'Mahony May 31 '17 at 19:21
  • Im in the process of moving my project to production. I installed Django-Report-Builder and it is working near perfectly. There are just some problems with accessing certain models. This is an attempt at a work around, and the last thing I need done before full live – Joe May 31 '17 at 19:28

1 Answers1

0

EDITED to include a better code snippet/example.

If you're not going to create a Django project but only use Django's ORM in your python file you need to create a settings.py file along with all your models and initialize Django's configuration like this:

from django.conf import settings
settings.configure(
    DATABASE_ENGINE = 'postgresql_psycopg2',
    DATABASE_NAME = 'db_name',
    DATABASE_USER = 'db_user',
    DATABASE_PASSWORD = 'db_pass',
    DATABASE_HOST = 'localhost',
    DATABASE_PORT = '5432',
    TIME_ZONE = 'America/Havana',
)

just before importing connection.

Check here and the official docs. There are some nice examples here too.

yorodm
  • 4,359
  • 24
  • 32
  • I do have a django project. I have the file I am writing in the same directory as my settings.py file which is why im confused. I have tried that command and it doesn't quite work. Gives me "please supply engine value" error – Joe May 31 '17 at 19:34
  • I'll try adding this but I have all of this defined in the local settings.py why can I not access it htere – Joe May 31 '17 at 19:40
  • If you do have a django project then your settings file is missing the `ENGINE` key. Take a look at [django settings reference](https://docs.djangoproject.com/en/1.11/ref/settings/#engine) – yorodm May 31 '17 at 19:40