根据1.4使用 Admin 与多个数据库的文档,您只需要在您的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
数据库。