Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django Rest Framework Without Database

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?

like image 845
Kian Ahrabian Avatar asked Aug 23 '16 09:08

Kian Ahrabian


People also ask

Can I use Django without a database?

Although you can use Django without a database, it comes with an object-relational mapper in which you describe your database layout in Python code.

Can we use Django REST framework without Django?

REST framework requires the following: Python (3.6, 3.7, 3.8, 3.9, 3.10) Django (2.2, 3.0, 3.1, 3.2, 4.0, 4.1)

Which is better for REST API flask or Django?

Flask has a broader approval, being mentioned in 502 company stacks & 509 developers stacks; compared to Django REST framework, which is listed in 159 company stacks and 77 developer stacks.

Can we create REST API using Django?

Creating a REST API in Django Using DRF. We are all set up with the Django project; now, we can start developing the domain model and business logic. First, we create the model simple model of Students to represent the student details. In the sample_app/model.py file, we define our model.


2 Answers

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,
}
like image 91
Kian Ahrabian Avatar answered Sep 19 '22 14:09

Kian Ahrabian


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.

like image 24
Andrea Corbellini Avatar answered Sep 23 '22 14:09

Andrea Corbellini