简体   繁体   English

使用现有的sqlite3数据库和Django

[英]Using an existing sqlite3 database with django

I am getting started with Django and have this particular question regarding using my own SQLite database with Django. 我正在开始使用Django,并且对于将我自己的SQLite数据库与Django结合使用存在特定问题。 What I mean is that if I create a new database and populate it everything works fine. 我的意思是,如果我创建一个新数据库并填充它,一切都会正常进行。 Here is my situation now. 这是我现在的情况。

I have an existing database in my instance retail_data.sqlite3 in the BASE_DIR. 我在BASE_DIR的实例retail_data.sqlite3中有一个现有数据库。 I now want to use this as the data so I go ahed and add this to seettings as follows. 我现在想将其用作数据,因此我将其添加到如下所示的填充物中。

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    },

    'retail_data':{
        'ENGINE':'django.db.backends.sqlite3',
        'NAME':os.path.join(BASE_DIR,'retail_data.sqlite3')

    }
}

The above piece of code is replicated from this site . 上面的代码段是从该站点复制的。 I then use the inspectdb command (python manage.py inspectdb --database=retail_data > salesapp/models.py) to generate the models.py which can use my current database. 然后,我使用inspectdb命令(python manage.py inspectdb --database = retail_data> salesapp / models.py)生成可以使用当前数据库的models.py。 Also I go ahead and update the admin.py file so that I can see it from my admin panel. 另外,我继续更新admin.py文件,以便可以从管理面板中看到它。 In the admin panel however even though the data table is visible I am unable to view the data. 但是在管理面板中,即使数据表可见,我也无法查看数据。

I was in doubt as to if the database actually consisted data so I used simple pandas.read_sql_query to query the data base and it has all the required data. 我对数据库是否真正包含数据感到怀疑,因此我使用简单的pandas.read_sql_query来查询数据库,并且数据库具有所有必需的数据。 How should I proceed? 我应该如何进行? Your guidance is much needed. 非常需要您的指导。 Thanks in advance 提前致谢

You will also need to tell Django which models use the database you've named 'default' , and which models use the database 'retail_data' . 您还需要告诉Django哪些模型使用您命名为'default'的数据库,哪些模型使用数据库'retail_data'

You do this by writing a database router class . 您可以通过编写数据库路由器类来实现

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

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