24

I'm trying to setup a simple API using Django Rest Framework, the problem is that my API does not have any database but the framework won't work without database setting.

Here is my Django Rest Framework configuration in settings.py:

INSTALLED_APPS = [
    'provider',
    'django_nose',
    'rest_framework',
    'django.contrib.contenttypes',
]

REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES': [],
    'DEFAULT_PERMISSION_CLASSES': [],
}

The error which I got is:

ImproperlyConfigured("settings.DATABASES is improperly configured. "django.core.exceptions.ImproperlyConfigured: settings.DATABASES is improperly configured. Please supply the ENGINE value. Check settings documentation for more details.

Is there any minimal settings which does not include django.contrib.contenttypes and django.contrib.auth?

6
  • When did you get that error ? Why do you need contenttypes application ? Commented Aug 23, 2016 at 10:06
  • Note that I have some API that don't use DB and haven't ran into that issue. Commented Aug 23, 2016 at 10:07
  • @Linovia I don't need contenttypes application and auth application, but it won't work without them, can you post a minimal django rest framework settings which does not hav any DB and will be only used as an API? Commented Aug 23, 2016 at 10:52
  • @Linovia It occurs when the view is trying to perform authentication Commented Aug 23, 2016 at 10:58
  • You can't have default authentication without DB. You'll have to write your own. Commented Aug 23, 2016 at 14:59

4 Answers 4

23

The actual cause of the problem is that DRF trys to add a user attribute to the request. Briefly mentioned in the documentation, the mechanism is as follows:

How authentication is determined

If no class authenticates, request.user will be set to an instance of django.contrib.auth.models.AnonymousUser

So it needed the django.contrib.auth application to run correctly, consequently django.contrib.auth requires a working configuration of Database to be able to perform.

The solution to this problem is to set the settings UNAUTHENTICATED_USER property to None.

Configuration will be like this after the changes:

REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES': [],
    'DEFAULT_PERMISSION_CLASSES': [],
    'UNAUTHENTICATED_USER': None,
}
Sign up to request clarification or add additional context in comments.

1 Comment

For me django rest framework APIView requires django.contrib.contenttypes, which is turned off. Error is RuntimeError: Model class django.contrib.contenttypes.models.ContentType doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS. The solution with UNAUTHENTICATED_USER works in the case too!
22

If you are really forced to use a database but you don't want to, you can use :memory: with the SQLite backend, like this:

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

This uses an in-memory database, so that your filesystem won't be touched.

Because memory is volatile, you might need to run migrations automatically every time your web app starts.

2 Comments

Can you describe more?
In short, instead of using disk space on server, you will use RAM. That is all. However, there is no need for doing this. Some suggested to use a file and you will not need to use it. You can use this option just to not see sqlite file and it will be deleted after shutting down. Both options are good to go.
2

You don't have any option. DATABASES dict should be in settings.py. You can use this:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}

4 Comments

I'm trying to setup the API without any database, even as small as sqlite :)
Why? Is there any reason for that?
It is redundant, there will be no another use for it.
Well, in this case you should create a big mock of database backend. Or extract model layer from database on your own from Django.
-1

[not tested] maybe you could use a dummy backend. I see there's one from django:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.dummy',
    }
}

1 Comment

ahhh hmm or maybe not.. github.com/django/django/blob/master/django/db/backends/dummy/… this is actually what it's complaining, you could probablly create one like this that just "pass" instead of raising an exception.. who knows it might work

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.