[FIXED] django admin specify database (not default)

Issue

I am using django admin to facilitate editing a database. Recently, we have added another database so using the default in DATABASES in the settings file would not make sense any more.

I now have this in my settings.py file:

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',
    },
}

My admin.py file contains:

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)

Initially, the admin section would connect with the default database. But now that I removed the default and added the other 2 databases, I'm getting the following error:

Django error.

If I copy the settings of animal_tracking to default, it works. My question therefore is:

How can I specify which database django admin should use?

Thank you!


Solution

You'd better specify the default database, if you leave default empty, you should write db routers.

in your admin, you can use any db you like:

class AnimalTypesAdmin(admin.ModelAdmin):
    using = 'animal_tracking'
    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)

save_model,delete_model,formfield_for_foreignkey,formfield_for_manytomany should also be overridden like this in the example.



Answered By - iMom0
Answer Checked By - Katrina (Python.Easybugfix Volunteer)

Related Posts

Post a Comment