正如@hynekcer所说,如果kayakodb
是现有数据库,则需要managed = False
为其所有模型进行设置.但是,这仍然会导致在错误的app(kayakodb
)内创建的代理模型的迁移问题.
可能有用的hacky修复方法是将app_label
代理模型更改为任何可以迁移的应用程序(sidebar
在本例中),并创建一个指向此代理模型的读取和写入的路由器kayakodb
.
例如代理模型:
# in sidebar/models.py class SidebarTicket(KayakoTicket): class Meta: proxy = True app_label = 'sidebar'
以及使用它的项目中的路由器:
from django.conf import settings from kayakodb.models import Ticket class ProxyDatabaseRouter(object): def allow_proxy_to_different_db(self, obj_): # check if this is a sidebar proxy to the Ticket model in kayakodb return isinstance(obj_, Ticket) and obj_._meta.proxy and obj_._meta.app_label == 'sidebar' def db_for_read(self, model, **hints): if issubclass(model, Ticket) and model._meta.proxy and model._meta.app_label == 'sidebar': return 'kayakodb' # the rest of the method goes here def db_for_write(self, model, **hints): if issubclass(model, Ticket) and model._meta.proxy and model._meta.app_label == 'sidebar': return 'kayakodb' return None # the rest of the method goes here def allow_relation(self, obj1, obj2, **hints): if self.allow_proxy_to_different_db(obj1) or self.allow_proxy_to_different_db(obj2): return True # the rest of the method goes here