3

I'm currently writing a Django template template tag, and I would like to be able to reference a class in it with a string, just like in settings.MIDDLEWARE_CLASSES, for example. The tag would look like {% mytag "myproject.lib.mymodule.SomeClass" %}.

The question is fairly simple : in my template tag, how may I easily resolve this string to the actual referenced class?

Thanks.

3
  • The answer is "yes". Now. Please edit the question to ask what you really what to know, which is "how do I transform a class name into a class object." Then we can ask "Why are you trying to do this?" Often, you'd be happier with some other technique. Please explain what your tag will do with the class object named as an argument. Commented Feb 8, 2012 at 21:22
  • I'd be interested in seeing how this is done, regardless of whether it's the best way of doing things or not Commented Feb 8, 2012 at 21:34
  • Thanks. The question was purely technical, and could be useful in many places, not necessarily in a template tag. But if you are curious, the goal is to create something similar to Facebook's BigPipe. The tag will take a Pagelet class path as argument, register it for deferred rendering, and finally return the placeholder, if necessary. Commented Feb 8, 2012 at 21:46

2 Answers 2

1

If you really need to import a package specified as a string, you will need to check out the __import__ function.

Basic usage is that you can attempt to import the package specified in the string like

my_class = __import__("myproject.lib.mymodule.SomeClass")

which is equivalent to

import myproject.lib.mymodule.SomeClass as my_class

However, as S.Lott mentioned, you'll need to consider carefully if that's really the best way to solve the problem.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks ! I considered many other options, but this one really seems to be the most flexible.
Just to add that the solution doesn't actually work as is, and I needed a little rsplit magic, and the fromlist argument of the __import__ function. I actually read this question to find the right answer.
1

Django has some utility functions for handling this situation.

In Django 1.7 or higher, use django.utils.module_loading.import_string function.

In Django 1.6, use django.utils.module_loading.import_by_path function.

(It looks like import_by_path exists in 1.5 as well, although it's not documented)

Example usage:

from django.utils.module_loading import import_string

my_class = import_string('myproject.lib.mymodule.SomeClass')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.