Commit 10da18b2 by Tom Christie

Access settings lazily, not at module import

parent 39ec564a
...@@ -786,7 +786,7 @@ class ModelSerializer(Serializer): ...@@ -786,7 +786,7 @@ class ModelSerializer(Serializer):
# you'll also need to ensure you update the `create` method on any generic # you'll also need to ensure you update the `create` method on any generic
# views, to correctly handle the 'Location' response header for # views, to correctly handle the 'Location' response header for
# "HTTP 201 Created" responses. # "HTTP 201 Created" responses.
url_field_name = api_settings.URL_FIELD_NAME url_field_name = None
# Default `create` and `update` behavior... # Default `create` and `update` behavior...
def create(self, validated_data): def create(self, validated_data):
...@@ -869,6 +869,9 @@ class ModelSerializer(Serializer): ...@@ -869,6 +869,9 @@ class ModelSerializer(Serializer):
Return the dict of field names -> field instances that should be Return the dict of field names -> field instances that should be
used for `self.fields` when instantiating the serializer. used for `self.fields` when instantiating the serializer.
""" """
if self.url_field_name is None:
self.url_field_name = api_settings.URL_FIELD_NAME
assert hasattr(self, 'Meta'), ( assert hasattr(self, 'Meta'), (
'Class {serializer_class} missing "Meta" attribute'.format( 'Class {serializer_class} missing "Meta" attribute'.format(
serializer_class=self.__class__.__name__ serializer_class=self.__class__.__name__
......
...@@ -26,7 +26,6 @@ from django.utils import six ...@@ -26,7 +26,6 @@ from django.utils import six
from rest_framework import ISO_8601 from rest_framework import ISO_8601
from rest_framework.compat import importlib from rest_framework.compat import importlib
USER_SETTINGS = getattr(settings, 'REST_FRAMEWORK', None)
DEFAULTS = { DEFAULTS = {
# Base API policies # Base API policies
...@@ -188,10 +187,17 @@ class APISettings(object): ...@@ -188,10 +187,17 @@ class APISettings(object):
and return the class, rather than the string literal. and return the class, rather than the string literal.
""" """
def __init__(self, user_settings=None, defaults=None, import_strings=None): def __init__(self, user_settings=None, defaults=None, import_strings=None):
self.user_settings = user_settings or {} if user_settings:
self._user_settings = user_settings
self.defaults = defaults or DEFAULTS self.defaults = defaults or DEFAULTS
self.import_strings = import_strings or IMPORT_STRINGS self.import_strings = import_strings or IMPORT_STRINGS
@property
def user_settings(self):
if not hasattr(self, '_user_settings'):
self._user_settings = getattr(settings, 'REST_FRAMEWORK', {})
return self._user_settings
def __getattr__(self, attr): def __getattr__(self, attr):
if attr not in self.defaults.keys(): if attr not in self.defaults.keys():
raise AttributeError("Invalid API setting: '%s'" % attr) raise AttributeError("Invalid API setting: '%s'" % attr)
...@@ -212,7 +218,7 @@ class APISettings(object): ...@@ -212,7 +218,7 @@ class APISettings(object):
return val return val
api_settings = APISettings(USER_SETTINGS, DEFAULTS, IMPORT_STRINGS) api_settings = APISettings(None, DEFAULTS, IMPORT_STRINGS)
def reload_api_settings(*args, **kwargs): def reload_api_settings(*args, **kwargs):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment