Django管理多个数据库

时间:2013-02-04 21:37:24

标签: django admin multiple-databases

根据使用Admin和多个数据库的1.4 documentation,您只需要在ModelAdmin子类中实现5个方法。我以推荐的方式覆盖了所有5个。浏览数据库没有问题。

但是,尝试保存现有记录并且我得到一个错误,声称数据库中不存在该表 - 项目的默认数据库,而不是我在方法实现。事实上,save_model()在抛出错误之前不会被调用,所以在它到达那里之前的某个地方,某个地方没有成功获得正确数据库的“使用”。

任何人都知道缺少什么?这是我的ModelAdmin课程:

class TransactionAdmin(admin.ModelAdmin):
    using = "salesdb"

    def save_model(self, request, obj, form, change):
        # Tell Django to save objects to the 'other' database.
        obj.save(using=self.using)

    def delete_model(self, request, obj):
        # Tell Django to delete objects from the 'other' database
        obj.delete(using=self.using)

    def queryset(self, request):
        # Tell Django to look for objects on the 'other' database.
        return super(TransactionAdmin, self).queryset(request).using(self.using)

    def formfield_for_foreignkey(self, db_field, request=None, **kwargs):
        # Tell Django to populate ForeignKey widgets using a query
        # on the 'other' database.
        return super(TransactionAdmin, self).formfield_for_foreignkey(db_field, request=request, using=self.using, **kwargs)

    def formfield_for_manytomany(self, db_field, request=None, **kwargs):
        # Tell Django to populate ManyToMany widgets using a query
        # on the 'other' database.
        return super(TransactionAdmin, self).formfield_for_manytomany(db_field, request=request, using=self.using, **kwargs)

我得到的错误是:

(1146, "Table 'django_dev._Transactions' doesn't exist")

django_dev是默认数据库,而不是sales数据库。

1 个答案:

答案 0 :(得分:0)

这显然是一个错误。

https://code.djangoproject.com/ticket/19747

该错误已被接受,所以我猜这意味着他们已经确认了。