2013-07-29 28 views
1

在我的一些django实用程序类中,我想定义一个主要方法,以便我可以调用我的模块而不必使用shell。 (致电​​)这甚至有可能吗?如果是这样,我怎么能打这个电话?不带壳的调用模块

from django.conf import settings 

... # Define module code here 

if __name__ == "__main__": 
    print(settings.DEBUG) 
    # interact with classes defined in this module which require django classes 
+2

编写自定义[django-admin命令](https://docs.djangoproject.com/en/1.5/howto/custom-management-commands/)。 – falsetru

+1

add'sys.path.append(“/ path/to/MyModule.py”)'那么你可以'导入MyModule' – Stephen

回答

1

这取决于你的意思。一般来说,您可以编写使用django基础架构的程序,并且与您的django应用程序分开运行(即它们不会提供任何内容),但仍可以例如影响数据库中的数据。你可以按照你的建议来做到这一点。

如果您想与正在运行的django服务交互,自定义命令可能更合适。