나는 django에서 파이썬 모듈을 동적으로 가져 오려고합니다. 나는에서 가져올 두 개의 서로 다른 애플 리케이션을 가지고 있고, 나는이 import 문 교체하려면 :Django - 동적으로 가져 오기
globals()[form]()
: 나는 문자열 App1ProfileForm 및 App2ProfileForm를 만든 다음과 같이 그것들을 초기화 할 동적으로 수 있어요
from app1.forms import App1ProfileForm
from app2.forms import App2ProfileForm
을
나는이 게시물의 지침 몇 가지 다음과 같은 시도 : Dynamically import class by name for static access
하고하는 것은 그래서 나는이 일을 시도 :
,651,하지만 난 App1ProfileForm 이름
편집 ::: 이 코드 시도 확인 없음 모듈 말한다 오류 받고 있어요 :
theModule = __import__("app1")
print theModule
theClass = getattr(theModule,'forms')
print theClass
theForm = getattr(theClass,'App1ProfileForm')
print theForm
theForm.initialize()
을하지만 오류가 해당 유형의 객체 App1ProfileForm '을 '초기화'속성이 없습니다.
는'theForm'는 클래스입니다. 그걸 사용하지 않고 인스턴스화하고 싶습니다. 'theForm(). initialize()'와 같은 것을 시도해보십시오. – pkoch