Hi, is it possible to write Django apps, for example for internal/personal use with existing databases, without having the 'overhead' of Djangos own tables that are usually installed when starting a project ? I would like to use existing tables via models, but not have all the other stuff that is surely useful on normal webpages. The reason would be to build small personal inspection/admin tools without being to invasive on legacy databases.
Django doesn't install any tables by itself. It comes with some pre-fabricated applications, which install tables, but those are easily disabled by removing them from the INSTALLED_APPS
setting.
Don't install any of Django's built-in apps and don't use any models.py
in your apps. Your database will have zero tables in it.
You won't have users, sites or sessions -- those are Django features that use the database.
AFAIK you should still, however, have a SQLite database. I think that parts of Django assume you've got a database connection and it may try to establish this connection.
It's an easy experiment to try.
The most noticable app with database tables defined is django.contrib.auth
, which implements its own auth backend in the database. You could probably skip auth all together if your app is firewalled and you trust all the people that have access to it. If otherwise, you want to create your own auth mechanism using existing infrastructure, you most likely want to use another backend. If your web-server sets REMOTE_USER
, you can use another builtin backend and you should be off and running. Otherwise you will have to implement your own to refer to other auth sources.
From there you only need to set up your models to use existing database tables instead of letting them make their own. You can have very fine control over this, for example
class MyModel(django.db.Model):
MyTextField = django.db.TextField(db_column="mytextfield", primary_key=True)
class Meta:
db_table = "my_table"
This way you can specify the exact table and columns from which each field of each model shall represent. Note that You can set the primary key to be a type other than integer. A limitation of django's ORM is that every model must have exactly one primary key, though. So if you don't have a primary key for your table, either add one, or you have to get along without the help of django's ORM.
Also, since you are tying in to an existing data-set, possibly that relates to other apps, you won't likely want to use ./manage.py syncdb
since it may do some undesireable things.
Just commentize the django app strings of the INSTALLED_APPS tuple in your project settings.py file (at the beginning of the project, before running syncdb).
You can also just add an extra database (set it as default) for keeping the extra django overhead stuff in:
DATABASES = {
'default': {
'ENGINE' : 'django.db.backends.sqlite',
'NAME' : 'djangoOverhead',
'USER' : '',
'PASSWORD' : '',
'HOST' : 'localhost' },
'legacyAppTables': {
'ENGINE' : 'django.db.backends.mysql',
'NAME' : 'legacyAppTables',
'USER' : '',
'PASSWORD' : '',
'HOST' : 'someRemoteHost' }, }