Django 从 sqlite3 切换到 Postgresql 问题

标签 django postgresql import

我最近将我项目中的数据库服务器从 sqlite3 更改为 Postgresql,我有几个问题希望能回答我的问题。

  1. 我知道从 sqlite 切换到 Postgres 意味着我创建了新数据库和其中的表,对吗?我已经这样做了,但是我没有看到在我的项目中创建任何新文件来告诉我我创建的数据库是可见的。 (顺便说一句,我已经在 settings.py 中更改了数据库名称)

  2. 我可能应该提到我在虚拟环境中工作,我想知道这是否会以任何方式影响我的推荐信。我试图在 Django 中导入表以尝试计算表中的记录数,但出现错误:“没有名为 psdemo 的模块”。 (psdemo 是我的数据库名称,我正在尝试导入表:

    from ps.psdemo import Product
    

    其中 ps 是我的应用程序,psdemo 是我的数据库,Product 是数据库中的表。

总而言之,我正在尝试访问我的数据库和表,但我找不到它们。我再说一遍,我的项目或我的虚拟环境中没有新的数据库文件(我已经彻底搜索过)但是如果我使用终端连接我可以连接到我的虚拟环境并更改目录以进入应用程序文件夹那么如果我连接到 Postgresql 服务器我可以创建数据库、表并可以插入其中、进行查询等,但我无法从 Django 代码访问它们。

最佳答案

I understand that switching from sqlite to Postgres implies that I create the new database and the tables inside it, right? I've done that but I haven't seen any new files created in my project to show me that the database I've made is visible. (Btw, I've changed the database name in settings.py)

使用 postgres 所要做的就是创建数据库。不是 table 。一旦您调用 syncdb,Django 将创建表以及它认为有用的任何其他内容。

您的项目中不会像在 sqlite 中那样有任何新文件。如果你想查看你的数据库,你应该下载并安装 pgadminIII(我无论如何都会推荐它)

I probably should mention that I'm working in a virtual environment and I would like to know if that affects my references in any way. I've tried to import the tables in Django to try and count the number of records in a table but I get the error: "No module named psdemo". (psdemo is my database name and i'm trying to import the table with:

在这里,您通过普通的 Python 语法导入模型,然后它引用您的表。每个模型都应该代表一个表。你先定义你的模型,然后调用

python manage.py syncdb

In conclusion I'm trying to get access to my database and tables but I can't manage to find them.

参见上文,但您绝对应该从 postgres 文档中阅读有关 postgres 安装的信息,并阅读 psycopg2 文档以及用于设置 postgres 数据库的 Django 文档。

关于Django 从 sqlite3 切换到 Postgresql 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9806085/

相关文章:

django - Google Calendar API 阻止 Django 启动

python - 使用来自 Celery tasks.py 的 Django Channels 向客户端发送消息

python - 在 IPython 之外使用 Anaconda 模块

django - 如何使 Django 模型仅在 Django Admin 中查看(只读)?

python - 以字符串形式返回模板 - Django

sql - 我是否需要使用函数来遍历唯一记录以进行批量更新?

PostgreSQL 9.6 : pgadmin4 is not opening on localhost:5050 (Ubuntu 16. 04)

sql - 如何将 accdb 转换为 postgres 数据库

mysql - 使用 PHPMyAdmin 导入 CSV 确实很麻烦

python - Pycharm,Jupyter Notebook,从不同的目录导入我自己的源文件