`REST_FRAMEWORK` in settings.py being ignored

574 views
Skip to first unread message

AJ Bowen

unread,
Oct 7, 2019, 7:13:53 AM10/7/19
to Django REST framework
I have a baffling problem: my DRF settings are not being taken into account.

In my settings.py, I have:

REST_FRAMEWORK = {"EXCEPTION_HANDLER": "api.exceptions.custom_exception_handler"}

^ This is simplified; I have a few other settings in this dict and they are all getting ignored unless I manually reload the settings. I have confirmed this by dropping into a debugger:

In [1]: from rest_framework.settings import api_settings                                                  

In [2]: api_settings.EXCEPTION_HANDLER                                                                    
Out[2]: <function rest_framework.views.exception_handler(exc, context)>

In [3]: api_settings.reload()                                                                             

In [4]: api_settings.EXCEPTION_HANDLER                                                                    
Out[4]: <function api.exceptions.custom_exception_handler(exc, context)>


Everything works as expected if I modify this line in the DRF source code, replacing:

        if not hasattr(self, "_user_settings"):
with
        if not hasattr(self, "_user_settings") or not self._user_settings:


It seems like my DRF settings are getting initialized before my django settings have a REST_FRAMEWORK attribute, and are therefore getting set to an empty dict...?

I am using Django 2.2.4 and DRF 3.10.3. (It was also happening into 3.10.2.)

This is driving me crazy, any help would be much appreciated.

AJ Bowen

unread,
Oct 7, 2019, 7:52:34 AM10/7/19
to Django REST framework
I found the problem. It was the presence of this line in my settings.py:

from rest_framework.pagination import PageNumberPagination

Maybe a circular import issue? I don't know, but once I remove that line, everything works as expected.
Reply all
Reply to author
Forward
0 new messages