Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed #19383 - load_class to django utils #557

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 17 additions & 0 deletions django/utils/importlib.py
Expand Up @@ -34,3 +34,20 @@ def import_module(name, package=None):
name = _resolve_name(name[level:], package, level)
__import__(name)
return sys.modules[name]

def load_class(path, package=None):
""" Load class by path

The 'package' argument is required when performing a relative import. It
specifies the package to use as the anchor point from which to resolve the
relative import to an absolute import.
"""
module_name, class_name = path.rsplit('.', 1)
module = import_module(module_name, package)
class_ref = getattr(module, class_name, None)
if not class_ref:
raise ImportError()
return class_ref