Django Shell 默认数据库

Django Shell Default Database

我有一个名为 steve_charts 的应用,其 models.py 包含 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'

这是指我正在访问的遗留数据库。在 settings.py 中,我的 postgresql 数据库已正确定义:

    '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',
    }
}

我运行python manage.py inspectdb --database steveDB > models.py给我一个新的models.py目前没问题...

I 运行 python manage.py makemigrations steve_charts(已添加到已安装的应用程序部分),一切顺利。 Django 访问了我的数据库,检查了 table,为我创建了一个新的 models.py 没问题。应用迁移没问题。

所以现在我想检查 shell:

中的数据库

python manage.py shell >>>from steve_charts.models import Atmospheric没问题。 >>>Atmospheric.objects.all() 这应该转储 table 中的所有行,对吧?相反,我得到 django.db.utils.OperationalError: no such table: atmospheric

完整堆栈跟踪:

>>> 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

我使用的是 RPi 3 B 型。Raspberry Pi OS 如果这有什么不同的话。

在我看来,shell 仅根据跟踪使用默认数据库。这是预期的行为还是我在这里是个愚蠢的家伙?

您可以在ORM查询中使用using()方法来指定要使用的数据库 像 Model.objects.using('steveDB').all()