Search code examples
djangodjango-modelsdjango-ormdjango-cache

Django in-memory model, not for tests


I have a model whose instances don't need to be written to database, ever. They are generated on the fly, very cheaply, and almost never change.

I wish to query these instances via the ORM and also browse them in django-admin (read-only + mass_actions, no need for CRUD).

From what I gathered, SQLite uses in-memory database when running tests. I think I want to use this feature, just not limited to tests.

Another option might be a pure in-memory model backend, but I'm not finding one. Actually I did find this project: https://github.com/felipecruz/dmqs It looks a bit dated though.

Or maybe there is a caching backend that can do this?


Solution

  • You have to use the special file name :memory: to instruct sqlite to use an in memory database.

    DATABASES = {
      'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': ':memory:',
      }
    }
    

    should do the trick.