从 Django 中的现有数据库生成一些模型

发布于 2024-08-27 07:36:00 字数 230 浏览 5 评论 0原文

我知道这种情况存在

django-admin.py inspectdb > models.py

但是,有没有简单的方法来限制它?无需手动删除我不想要的内容。

我正在连接到一个拥有一百多个表的数据库,但我只想要大约 4 或 5 个表的模型。是否有一种简单的方法可以从几个给定的表生成模型?

它们是相当大的桌子,所以我也不喜欢把它们全部打出来。

I know this exists

django-admin.py inspectdb > models.py

However, is there an easy way to limit it? Without manually deleting what I don't want.

I'm connecting to a database that has over one hundred tables, but I only want models of about 4 or 5. Is there an easy way to generate models from a few given tables?

They are quite big tables, so I don't fancy typing them all out either.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

梦幻之岛 2024-09-03 07:36:00

我自己也是这样做的,也是与 Oracle 一起做的。这是可能的——但并不美好。

假设您知道所需表的名称 -

打开 django/db/backends/oracle/introspection.py 。有一个函数 get_table_list

def get_table_list(self, cursor):
    "Returns a list of table names in the current database."
    cursor.execute("SELECT TABLE_NAME FROM USER_TABLES")
    return [row[0].lower() for row in cursor.fetchall()]

只需将其替换为类似的

def get_table_list(self, cursor):
    names = ['mytable1', 'mytable2', 'mytable3']
    return names

然后运行您的 inspectdb ,它将更易于管理:)

I just did this myself, also with Oracle. It's possible - but not pretty.

Assuming you know the names of the tables you want -

open django/db/backends/oracle/introspection.py. There is a function get_table_list:

def get_table_list(self, cursor):
    "Returns a list of table names in the current database."
    cursor.execute("SELECT TABLE_NAME FROM USER_TABLES")
    return [row[0].lower() for row in cursor.fetchall()]

Just replace it with something like

def get_table_list(self, cursor):
    names = ['mytable1', 'mytable2', 'mytable3']
    return names

Then run your inspectdb and it will be a lot more managable :)

写下不归期 2024-09-03 07:36:00

不要使用syncdb>模型.py。这不是一个好的做法。手动创建模型并向其中添加 management=False。如果您不添加它,则可以通过单个命令删除所有数据库表。创建模型后,运行 syncdb 以便链接表。

Do not use syncdb > models.py. It's not a good practice. Make your models manually and add managed=False to it. If you will not add it your all database tables can be deleted via single command. After creating your models then run the syncdb so that tables are linked.

触ぅ动初心 2024-09-03 07:36:00

@pfctdayelise 给出的以下解决方案

对于 django 1.8 mysql 后端

打开 django/db/backends/mysql/introspection.py 并查找函数 get_table_list

def get_table_list(self, cursor):
    cursor.execute("SHOW FULL TABLES")
    return [TableInfo(row[0], {'BASE TABLE': 't', 'VIEW': 'v'}.get(row[1]))
            for row in cursor.fetchall()]

替换

def get_table_list(self, cursor):
    names = [TableInfo('mytable1', 't')]
    return names

要确定 TableInfo 的第二个参数是 t 还是 v,请运行 mysql 查询 SHOW FULL TABLES code> 并找出您的 table_type 如果它是 BASE_TABLE 那么第二个参数是 t else v

然后运行

python manage.py inspectdb > models.py

Following solution given by @pfctdayelise

For django 1.8 mysql backend

open django/db/backends/mysql/introspection.py and find function get_table_list:

def get_table_list(self, cursor):
    cursor.execute("SHOW FULL TABLES")
    return [TableInfo(row[0], {'BASE TABLE': 't', 'VIEW': 'v'}.get(row[1]))
            for row in cursor.fetchall()]

Replace it with something like

def get_table_list(self, cursor):
    names = [TableInfo('mytable1', 't')]
    return names

To decide whether the second argument to TableInfo is t or v, run the mysql query SHOW FULL TABLES and find out your table_type if it is a BASE_TABLE then second argument is t else v

Then run

python manage.py inspectdb > models.py
幸福%小乖 2024-09-03 07:36:00

从 Django 1.10 开始,inspectdb 命令在命令行上采用可选的表列表来限制将检查哪些表。

Starting in Django 1.10, the inspectdb command takes an optional list of tables on the command line that limits which tables will be inspected.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文