Using Django as an easy to setup admin interface for existing DBΒΆ


  • The project uses a DB Backend Django supports
  • You have Python and Virtualenvwrapper installed -
  • You have an existing project in any programming language (could be NodeJS / Ruby / Php / Python etc..).
  • In this guide the existing project is called “spark”

Python project setup

  • run from the root directory of the project:
mkvirtualenv -p python3 spark-django
workon spark-django
echo "Django" >> requirements.txt
pip install -r requirements.txt
django-admin startproject djspark ./

Support for local settings

echo "from .settings import *" > djspark/
echo "INSTALLED_APPS += ['djspark']" >> djspark/
echo "/" >> djspark/.gitignore
  • edit - change djspark.settings to djspark.local_settings
  • edit spark/ - change djspark.settings to djspark.local_settings

Connect to the existing DB

  • add you DB configuration to djspark/
  • for mysql it will be something like this:
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'mydatabase',
        'USER': 'mydatabaseuser',
        'PASSWORD': 'mypassword',
        'HOST': '',
        'PORT': '5432',
  • Refer to Django documentation for details
  • You will also need to install the relevant database driver:
echo "mysqlclient" >> requirements.txt
pip install -r requirements.txt

Setup admin for all existing DB models

./ inspectdb > djspark/
  • Review djspark/ file and delete irrelevant models / update fields
  • edit djspark/ - paste the following and modify according to your models and admin requirements
from django.contrib import admin
from .models import *

class AutoModelAdmin(admin.ModelAdmin):

    list_display = ()
    search_fields = ()
    list_hide = ()
    search_exclude = ()

    def __init__(self, model, admin_site, list_hide=(), search_exclude=()):
        if self.list_hide == ():
            self.list_hide = list_hide
        if self.search_exclude == ():
            self.search_exclude = search_exclude
        if self.list_display == ():
            self.list_display = self.get_default_list_display(model)
        if self.search_fields == ():
            self.search_fields = self.get_default_search_fields(model)
        super(AutoModelAdmin, self).__init__(model, admin_site)

    def get_default_list_display(self, model):
        return [ for f in model._meta.fields if not in self.list_hide]

    def get_default_search_fields(self, model):
        return [ for f in model._meta.fields if not in self.search_exclude]

    def get_class(**kwargs):
        def auto_model_admin_class_generator(model, admin_site):
            return AutoModelAdmin(model, admin_site, **kwargs)
        return auto_model_admin_class_generator, AutoModelAdmin), AutoModelAdmin), AutoModelAdmin), AutoModelAdmin), AutoModelAdmin.get_class(list_hide=('password',)))


./ migrate
  • at this point you might get errors regarding your models
  • best fix in this case is to comment-out the problematic fields

create user and log-in

./ createsuperuser
./ runserver

next steps