Django is an all-in one solution for building interactive web sites. It’s very good for a typical setup with a web server and a database. But what if you don’t need a database, for example if you get all the data through external services? Or if you’re using Django with a NoSQL database, not using Django’s built-in support for data models?
It is possible to run Django without setting up a database. In settings.py, just set DATABASES to an empty dictionary:
DATABASES = {}
Much of the default functionality in Django expects there to be a database. The following common applications will not work with this configuration because they require a database:
- auth
- contenttypes
- sites
These applications have to be disabled.
The following applications can be used without a database:
- sessions: Can be configured to use a file or the cache instead of the database.
- messages: Does not use any storage directly, but depends on sessions by default. This can be configured though.
- staticfiles: Does not use the database.
The only problem left is that Django’s test runner sets up a
database when you run the command manage.py test. It
is simple to disable that behaviour.
Just create the file testing.py with the following
contents:
"""Support for testing."""
from django.test.simple import DjangoTestSuiteRunner
class DatabaselessTestRunner(DjangoTestSuiteRunner):
"""A test suite runner that does not set up and tear down a database."""
def setup_databases(self):
"""Overrides DjangoTestSuiteRunner"""
pass
def teardown_databases(self, *args):
"""Overrides DjangoTestSuiteRunner"""
pass
Then in settings.py, make the following setting:
TEST_RUNNER = 'testing.DatabaselessTestRunner'
That’s it. Now the test runner will not set up and tear down a database.
Even though you can’t use Django’s models (the object/relational
mapper), you will still need to create a file called
models.py in the application directory. This is so the
test runner searches for the tests in the corresponding
tests.py file. The models.py file can be
empty.