From 0d47f4fc80d52bdbbea839cd5a1320ede14bca92 Mon Sep 17 00:00:00 2001 From: Jon Dufresne Date: Mon, 29 Apr 2019 17:57:43 -0700 Subject: [PATCH] Simplify import_from_string() with Django's import_string() --- rest_framework/settings.py | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/rest_framework/settings.py b/rest_framework/settings.py index 5d92d0cb4..1d5dc036f 100644 --- a/rest_framework/settings.py +++ b/rest_framework/settings.py @@ -18,10 +18,9 @@ This module provides the `api_setting` object, that is used to access REST framework settings, checking for user settings first, then falling back to the defaults. """ -from importlib import import_module - from django.conf import settings from django.test.signals import setting_changed +from django.utils.module_loading import import_string from rest_framework import ISO_8601 @@ -175,11 +174,8 @@ def import_from_string(val, setting_name): Attempt to import a class from a string representation. """ try: - # Nod to tastypie's use of importlib. - module_path, class_name = val.rsplit('.', 1) - module = import_module(module_path) - return getattr(module, class_name) - except (ImportError, AttributeError) as e: + return import_string(val) + except ImportError as e: msg = "Could not import '%s' for API setting '%s'. %s: %s." % (val, setting_name, e.__class__.__name__, e) raise ImportError(msg)