简体   繁体   English

将Django与包含多个表的旧数据库一起使用

[英]Using Django with legacy databases containing multiple tables

I have an old application written in php that provides access to large datasets that are organized into 37 different databases, with each database containing a number of tables. 我有一个用php编写的旧应用程序,该应用程序提供对组织到37个不同数据库中的大型数据集的访问,每个数据库包含多个表。 Each table represents data from a different piece of equipment, and each database represents the data from a single time period, so the structure is as follows: 每个表代表来自不同设备的数据,每个数据库代表来自单个时间段的数据,因此结构如下:

Database_Time_Period_one   
 table machine 1
 table machine 2
 table machine 3
 and so on...

Database_Time_Period_two
 table machine 1
 table machine 2
 table machine 3
 table machine 4
 and so on...

The tables vary in length but typically contain between 500,000 and 1,000,000 rows of data. 这些表的长度各不相同,但通常包含500,000到1,000,000行之间的数据。

The application needs to be extended and needs a new front end, and am looking at reworking it using Django, but I am having a conceptual problem which I am hoping someone can help with. 该应用程序需要扩展,并且需要一个新的前端,并且正在考虑使用Django对其进行重做,但是我遇到了一个概念性的问题,希望有人可以提供帮助。 The php code is very simple - the user selects the time period and table of interest in the front end, and this is used to build a MySQL SELECT query that retrieves the relevant data which is then stored in an array object. php代码非常简单-用户在前端选择时间段和感兴趣的表,这用于构建MySQL SELECT查询,该查询检索相关数据,然后将其存储在数组对象中。 The query is constructed in a single line of code that essentially uses a variable taken from the front end to identify the database, and another for the table. 该查询是用单行代码构造的,该代码行实际上使用从前端获取的变量来标识数据库,而另一个用于表。 The naming of the databases and tables follows a pattern that includes a sequential numeric component and this simplifies the construction of the query. 数据库和表的命名遵循一种模式,该模式包括一个连续的数字部分,这简化了查询的结构。

As I understand things from my initial forays into Django, a django model represents a single table from a single database, and so to be able to access all of my tables across the different databases, I would need to create a model for each table. 当我从最初进入Django的过程中了解到事情时,django模型代表来自单个数据库的单个表,因此为了能够访问不同数据库中的所有表,我需要为每个表创建一个模型。 This would mean having around 200 models. 这意味着大约有200个模型。 Also, I would need some way to tell django which database and which table in that database the model should be linked to. 另外,我需要某种方式告诉django模型应该链接到哪个数据库以及该数据库中的哪个表。 Finally, I would have to be able to select in code which model to use for any given query. 最后,我必须能够在代码中选择用于任何给定查询的模型。 I have looked into this and it seems that it would be possible to do this, but it would mean creating a large number of routers, and a lot of code compared to the relatively simple php code. 我已经研究过了,似乎有可能做到这一点,但是与相对简单的php代码相比,这意味着要创建大量的路由器和许多代码。

Is it possible to have simple code that would essentially tell django: "Go to database x, look at table y, and select the following columns" even if this mean circumventing the use of a model and just reading the data into an array? 是否有可能从本质上告诉django的简单代码:“转到数据库x,查看表y,然后选择以下列”,即使这意味着绕过模型的使用而只是将数据读入数组中?

I have searched online for a solution to this, but can't find an example that appears to address this situation, and while the django documentation does explain the use of multiple databases, it does not seem to provide a simple way to access the databases that I have without creating what looks like a very large and cumbersome body of code, and the explanation of using raw queries still seems to rely on the 'one model per table' paradigm. 我已经在网上搜索了解决方案,但是找不到一个可以解决这种情况的示例,尽管django文档确实说明了多个数据库的使用,但它似乎并未提供访问数据库的简单方法我没有创建看起来非常庞大和繁琐的代码体,并且使用原始查询的解释似乎仍然依赖于“每表一个模型”范例。 I am sure that my difficulty is due to my limited experience with Django and that there is a way to do what I want, but I at the moment I am stuck. 我确信我的困难归因于我对Django的有限经验,并且有一种方法可以做我想做的事情,但是现在我被困住了。

It would be very helpful is someone with considerably more django experience than I have could point me in the right direction, or tell me if this looks like a situation that django is just not suited to. 如果某人的django经验比我所能指示的方向要正确得多,或者告诉我这是否看起来像django不适合的情况,那将非常有帮助。

Do you know about django's inspectdb management command: 您是否知道django的inspectdb管理命令:

$ python manage.py inspectdb > models.py

This will produce models for you from which you can use the django orm to do whatever you need to do with your data. 这将为您生成模型,您可以根据这些模型使用django orm进行数据处理。

http://docs.djangoproject.com/en/dev/ref/django-admin/#inspectdb http://docs.djangoproject.com/en/dev/ref/django-admin/#inspectdb

https://docs.djangoproject.com/en/1.7/howto/legacy-databases/ https://docs.djangoproject.com/en/1.7/howto/legacy-databases/

You can write sql code directly and not make models if you think that is easier. 如果您认为更简单,则可以直接编写sql代码,而不创建模型。 Look here: https://docs.djangoproject.com/en/1.7/topics/db/sql/#executing-custom-sql-directly 在这里查看: https//docs.djangoproject.com/en/1.7/topics/db/sql/#executing-custom-sql-direct

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM