Django Shell Default Database

Viewed 192

I have an app named steve_charts with a models.py containing the class Atmospheric:

class Atmospheric(models.Model):
    time = models.DateTimeField(unique=True)
    temperature = models.IntegerField(blank=True, null=True)
    humidity = models.IntegerField(blank=True, null=True)
    kpa = models.IntegerField(blank=True, null=True)    

    class Meta:
        managed = False
        db_table = 'atmospheric'

This refers to a legacy DB I am accessing. In settings.py my postgresql DB is correctly defined:

    'steveDB':{
        'ENGINE': 'django.db.backends.postgresql',
        'HOST': 'localhost',
        'NAME': 'sensor_data',
        'USER': 'pi'
    },
    'default': { ## NOT EVEN USING THIS THING
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': BASE_DIR / 'db.sqlite3',
    }
}

I ran python manage.py inspectdb --database steveDB > models.py to get me a fresh new models.py no problems so far...

I ran python manage.py makemigrations steve_charts (which has been added to the installed apps section) and everything went smoothly. Django accessed my DB, inspected the table, made a new models.py for me no problem. Applied migrations no problem.

So now I would like to check the DB in the shell:

python manage.py shell >>>from steve_charts.models import Atmospheric No problems. >>>Atmospheric.objects.all() This should dump all the rows in the table, right? Instead I get django.db.utils.OperationalError: no such table: atmospheric

Full stack trace:

>>> Atmospheric.objects.all()
Traceback (most recent call last):
  File "/home/pi/steve/env/lib/python3.7/site-packages/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
  File "/home/pi/steve/env/lib/python3.7/site-packages/django/db/backends/sqlite3/base.py", line 423, in execute
    return Database.Cursor.execute(self, query, params)
sqlite3.OperationalError: no such table: atmospheric

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/home/pi/steve/env/lib/python3.7/site-packages/django/db/models/query.py", line 256, in __repr__
    data = list(self[:REPR_OUTPUT_SIZE + 1])
  File "/home/pi/steve/env/lib/python3.7/site-packages/django/db/models/query.py", line 280, in __iter__
    self._fetch_all()
  File "/home/pi/steve/env/lib/python3.7/site-packages/django/db/models/query.py", line 1324, in _fetch_all
    self._result_cache = list(self._iterable_class(self))
  File "/home/pi/steve/env/lib/python3.7/site-packages/django/db/models/query.py", line 51, in __iter__
    results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
  File "/home/pi/steve/env/lib/python3.7/site-packages/django/db/models/sql/compiler.py", line 1175, in execute_sql
    cursor.execute(sql, params)
  File "/home/pi/steve/env/lib/python3.7/site-packages/django/db/backends/utils.py", line 98, in execute
    return super().execute(sql, params)
  File "/home/pi/steve/env/lib/python3.7/site-packages/django/db/backends/utils.py", line 66, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "/home/pi/steve/env/lib/python3.7/site-packages/django/db/backends/utils.py", line 75, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "/home/pi/steve/env/lib/python3.7/site-packages/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
  File "/home/pi/steve/env/lib/python3.7/site-packages/django/db/utils.py", line 90, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/home/pi/steve/env/lib/python3.7/site-packages/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
  File "/home/pi/steve/env/lib/python3.7/site-packages/django/db/backends/sqlite3/base.py", line 423, in execute
    return Database.Cursor.execute(self, query, params)
django.db.utils.OperationalError: no such table: atmospheric
>>> django.db.utils.OperationalError: no such table: atmospheric
  File "<console>", line 1
    django.db.utils.OperationalError: no such table: atmospheric
                                            ^
SyntaxError: invalid syntax

I'm using a RPi 3 model B. Raspberry Pi OS if that makes any difference.

Seems to me that the shell is only using the default DB as per trace. Is this expected behavior or am I being a stupid fella here?

1 Answers

You can use using() method in ORM query to specify database to use Like Model.objects.using('steveDB').all()

Related