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

Prerequisites

  • The project uses a DB Backend Django supports
  • You have Python and Virtualenvwrapper installed - https://virtualenvwrapper.readthedocs.io/en/latest/
  • 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/local_settings.py
echo "INSTALLED_APPS += ['djspark']" >> djspark/local_settings.py
echo "/local_settings.py" >> djspark/.gitignore
  • edit manage.py - change djspark.settings to djspark.local_settings
  • edit spark/wsgi.py - change djspark.settings to djspark.local_settings

Connect to the existing DB

  • add you DB configuration to djspark/local_settings.py
  • for mysql it will be something like this:
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'mydatabase',
        'USER': 'mydatabaseuser',
        'PASSWORD': 'mypassword',
        'HOST': '127.0.0.1',
        '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

./manage.py inspectdb > djspark/models.py
  • Review djspark/models.py file and delete irrelevant models / update fields
  • edit djspark/admin.py - 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 [f.name for f in model._meta.fields if f.name not in self.list_hide]

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

    @staticmethod
    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


admin.site.register(CampDetails, AutoModelAdmin)
admin.site.register(Camps, AutoModelAdmin)
admin.site.register(NpoMembers, AutoModelAdmin)
admin.site.register(Payments, AutoModelAdmin)
admin.site.register(Users, AutoModelAdmin.get_class(list_hide=('password',)))

migrate

./manage.py 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

./manage.py createsuperuser
./manage.py runserver

next steps