【发布时间】:2014-01-09 14:00:36
【问题描述】:
我正在使用 django admin 来帮助编辑数据库。最近,我们添加了另一个数据库,因此在设置文件中使用 DATABASES 中的默认值将不再有意义。
我现在在我的 settings.py 文件中有这个:
DATABASES = {
'default': {},
'animal_tracking': {
'ENGINE':'django.db.backends.mysql',
'NAME': 'AnimalTracking',
'USER': 'foo',
'PASSWORD': 'bar',
'HOST': '127.0.0.1',
'PORT': '3306',
},
'animal_information': {
'ENGINE':'django.db.backends.mysql',
'NAME': 'AnimalInformation',
'USER': 'foo',
'PASSWORD': 'bar',
'HOST': '127.0.0.1',
'PORT': '3306',
},
}
我的 admin.py 文件包含:
from django.contrib import admin
from animal_tracking.models import at_animal_types, at_animals
# Register your models here.
class AnimalTypesAdmin(admin.ModelAdmin):
# Sets how the fields are displayed in the add / change section.
fieldsets = [
(None, {'fields': ['type', ]}),
]
# Sets what fields to be displayed in the change (view) section.
list_display = ('type', )
# Registers the current model administration to the admin page.
admin.site.register(at_animal_types, AnimalTypesAdmin)
class AnimalsAdmin(admin.ModelAdmin):
# Sets how the fields are displayed in the add / change section.
fieldsets = [
(None, {'fields': ['tracker_id', 'type', ]}),
('Log information (should not change)', {'fields': ['last_log', 'last_bat', 'last_lat', 'last_lon', ], 'classes': ['collapse']}),
]
# Sets what fields to be displayed in the change (view) section.
list_display = ('tracker_id', 'type', 'last_log', 'last_bat', 'last_lat', 'last_lon', )
# Sets what fields to allow filtering by.
list_filter = ['type', ]
# Sets what fields to allow searching by. Use table__field if foreign key.
search_fields = ['tracker_id', 'type__type', ]
# Registers the current model administration to the admin page.
admin.site.register(at_animals, AnimalsAdmin)
最初,管理部分将连接到默认数据库。但是现在我删除了默认值并添加了其他 2 个数据库,我收到以下错误:
如果我将 animal_tracking 的设置复制到默认值,它就可以工作。因此,我的问题是:
我如何指定 django 管理员应该使用哪个数据库?
谢谢!
【问题讨论】:
标签: django django-models