Skip to content

Commit

Permalink
Simplify import_from_string() with Django's import_string() (#6617)
Browse files Browse the repository at this point in the history
  • Loading branch information
jdufresne authored and rpkilby committed May 1, 2019
1 parent 1c976f2 commit e16273a
Showing 1 changed file with 3 additions and 7 deletions.
10 changes: 3 additions & 7 deletions rest_framework/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,9 @@
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

Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit e16273a

Please sign in to comment.