简体   繁体   English

如何从外部py脚本访问Models.py?

[英]How to access Models.py from external py script?

I want to integrate my existing code into django. 我想将现有代码集成到Django中。 How can I access models.py from external code.I tried putting it in the same application directory but it did not work. 我该如何从外部代码访问models.py,我试图将其放在同一应用程序目录中,但没有用。

Case Scenario : Having a web parsing code that takes data from the web and prints it. 案例场景 :拥有一个Web解析代码,该代码从Web上获取数据并进行打印。 Here I am integrating it with django so as to save that data in the db and print it out in the web page. 在这里,我将其与django集成在一起,以便将该数据保存在db中并在网页中打印出来。 I'm trying the input to get from the webpage and when I hit submit it triggers the parsing code and gives output on the page. 我正在尝试从网页获取输入,当我点击提交时,它将触发解析代码并在页面上提供输出。

I have the Django server up and running, created the apps (startapp), have the table created via models.py, have the urls.py setup. 我已经启动并运行Django服务器,创建了应用程序(startapp),通过models.py创建了表,并设置了urls.py。 Here my question is how or where do I put my external code to make it trigger to access models .py, will it execute by itself when I start the server and hit the submit button ( i know i have to configure this one). 在这里,我的问题是如何或在哪里放置我的外部代码,以使其触发访问模型.py,当我启动服务器并单击“提交”按钮时,它是否会自行执行(我知道我必须配置此代码)。

There are a few lines you need to include in your script if you want to use Django in "standalone" mode. 如果要在“独立”模式下使用Django,则需要在脚本中包含几行。 See the documentation here: https://docs.djangoproject.com/en/1.10/topics/settings/#calling-django-setup-is-required-for-standalone-django-usage . 请参阅此处的文档: https : //docs.djangoproject.com/en/1.10/topics/settings/#calling-django-setup-is-required-for-standalone-django-usage

Another option would be to turn your script into a Django management command which you would then call with manage.py . 另一个选择是将脚本转换为Django管理命令,然后使用manage.py进行调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM