小编mta*_*lha的帖子

在 Django 中使用多个数据库

我必须在我的项目中使用多个数据库,但有一个问题。模型看不到我的数据库。

当我去 localhost:port/admin/app/accounts 它说我没有这样的表:帐户。模型寻找默认数据库。不是accounts.db 我该怎么做?

我在互联网上看到的内容中尝试了多种方法,但没有任何反应。

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

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


models.py:
.....
class Accounts(models.Model):     

    email = models.TextField(blank=True, null=True)                                                                                                                    
    phone = models.TextField(blank=True, null=True)                                                                                                                    
    name = models.TextField(blank=True, null=True)                                                                                                                     
    password = models.TextField(blank=True, null=True)                                                                                                                 
    verify = models.IntegerField(blank=True, null=True)                                                                                                                

    class Meta:                                                                                                                                                        
        managed = False

        db_table = 'accounts'
....


Environment:


Request Method: GET
Request URL: http://localhost:port/admin/hede/accounts/

Django Version: 1.11.23
Python Version: 2.7.15
Installed Applications:
['django.contrib.admin',
 'django.contrib.auth', …
Run Code Online (Sandbox Code Playgroud)

python django python-3.x django-2.0

8
推荐指数
3
解决办法
2万
查看次数

标签 统计

django ×1

django-2.0 ×1

python ×1

python-3.x ×1