I have a pre-existing db file 'sample.db' in sqlite3 format that I'm trying to import into my Django project. I've already added it to the settings.py file:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
},
'sample': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'sample.db',
}
I was able to generate the model.py in my application folder using inspectdb. I have run makemigrations and migrate and everything seems to be fine.
I try to get some data in my views.py: obj = table_name.objects.get(id=1)
Then I start the server: python manage.py runserver, and try to load a page at http://127.0.0.1:8000/
It displays an error message saying: NameError at / Name table_name is not defined
What did I miss?
Thank you!
As you've added your sample database to your settings as sample
you need to ensure your queries are using the correct database.
For example obj = table_name.objects.get(id=1).using('sample')
The alternative would be to configure your database to be the default
setting.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.