这是我对Stackoverflow的第一个问题。如果出现任何问题,请告诉我。非常感谢!!
我按照django官方文件,但仍然无法工作。
在shell中,它可以显示来自不同数据库的数据,这意味着路由器可以很好地工作。
但它无法在管理站点中运行。 image captures here
或者引发“LookupError”“没有安装带有标签'洞察'的应用。”当我从
改变othersite.register(CrawlerIndex, CrawlerIndexAdmin)
到
admin.site.register(CrawlerIndex, CrawlerIndexAdmin)
在admin.py中
这是我的admin.py
from django.contrib import admin
from models import MonitorCrawlers, CrawlerIndex
# Register your models here.
admin.autodiscover()
class MultiDBModelAdmin(admin.ModelAdmin):
# A handy constant for the name of the alternate database.
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 get_queryset(self, request):
# Tell Django to look for objects on the 'other' database.
return super(MultiDBModelAdmin, self).get_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(MultiDBModelAdmin, 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(MultiDBModelAdmin, self).formfield_for_manytomany(db_field, request=request, using=self.using, **kwargs)
class MultiDBModelAdmin2(MultiDBModelAdmin):
using = 'insight_db'
class MonitorCrawlersAdmin(admin.ModelAdmin):
pass
class CrawlerIndexAdmin(MultiDBModelAdmin2):
pass
othersite = admin.AdminSite('insight_db')
othersite.register(CrawlerIndex, CrawlerIndexAdmin)
admin.site.register(MonitorCrawlers, MonitorCrawlersAdmin)
和setting.py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'table',
'table.templatetags.extfilter',
'django.contrib.humanize',
]
DATABASE_APPS_MAPPING = {
'insight': 'insight_db',
}
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'myapp1',
'USER': '',
'PASSWORD': '',
'HOST': 'xxx.xxx.xxx.73',
'PORT': '3306',
},
'insight_db': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'crawler',
'USER': '',
'PASSWORD': '',
'HOST': 'xxx.xxx.xxx.73',
'PORT': '3306',
}
}
DATABASE_ROUTERS = ['table.db_router.InsightRouter',]
和InsightRouter.py
class InsightRouter(object):
"""
A router to control all database operations on models in the
insight_db application.
"""
def db_for_read(self, model, **hints):
"""
Attempts to read insight_db models go to insight_db.
"""
if model._meta.app_label == 'insight':
return 'insight_db'
return None
def db_for_write(self, model, **hints):
"""
Attempts to write insight_db models go to insight_db.
"""
if model._meta.app_label == 'insight':
return 'insight_db'
return None
def allow_relation(self, obj1, obj2, **hints):
"""
Allow relations if a model in the insight_db app is involved.
"""
if obj1._meta.app_label == 'insight' or \
obj2._meta.app_label == 'insight_db':
return True
return None
def allow_migrate(self, db, app_label, model_name=None, **hints):
"""
Make sure the insight_db app only appears in the 'insight_db'
database.
"""
if app_label == 'insight':
return db == 'insight_db'
return None
和models.py
class CrawlerIndex(models.Model):
blahblahblah
class Meta:
app_label = 'insight'
managed = False
db_table = 'crawler_index'