在我的django项目中无法使用多个数据库

问题描述 投票:0回答:1

我正在尝试为我的django项目使用两个DB。第一个用于认证等,第二个用于保存用户通过表单发送的数据。

我将第二个数据库添加到我的settings.py文件中,但我一直收到错误,最近的一个是(1146, "Table 'dataset.main_SomeModel' doesn't exist")

实际上,看起来我的Django项目无法与db交互,因为那里没有表。

难道我做错了什么?也许在这里使用两个DB是错误的方法?

这是settings.py,第二个名为dataset的数据库是我正在尝试使用的数据库:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    },
    'dataset': {
        'NAME': 'dataset',
        'ENGINE': 'django.db.backends.mysql',
        'USER': 'root',
        'PASSWORD': 'password goes here'
    }
}

这是模型:

class SomeModel(models.Model):
    data = models.CharField(max_length=100)
    num = models.Float()

    def save(self): # ALL the signature         
        super(SomeModel, self).save(using='dataset')

以下是表格:

class DataForm(forms.ModelForm):

    class Meta:
        model = SomeModel
        fields = ("data", "num")

    def save(self, commit=True):
        send = super(DataForm, self).save(commit=False)
        if commit:
            send.save()
        return send

既然我添加了行using="dataset"不应该将数据发送到dataset db?还是我做错了什么?任何建议表示赞赏!

编辑:我尝试使用manage.py migrate --database="dataset"迁移第二个数据库,但我得到错误The connection dataset doesn't exist

python django django-models django-forms django-database
1个回答
0
投票

你缺少引用'。试试这个

super(SomeModel, self).save(using='dataset')

你可以查找这个Multi DB Save

© www.soinside.com 2019 - 2024. All rights reserved.