如何在龙卷风应用程序中使用Django ORM? [英] How can I use the Django ORM in my Tornado application?

查看:94
本文介绍了如何在龙卷风应用程序中使用Django ORM?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个现有的Django应用程序与数据库和相应的 models.py 文件。



我有一个新的Tornado应用程序提供Web服务到其他应用程序。它需要从同一个数据库中读取/写入,并且我想使用的模型文件中有代码。



我如何最好地使用Django数据库我的龙卷风请求处理程序中的模型?是否像简单的链接到 models.py Django项目文件夹,导入Django模块和使用它?我想我必须做 settings.configure(),对吧?



谢谢!

解决方案

将Django项目的路径添加到Tornado应用程序的PYTHONPATH env-var并适当设置DJANGO_SETTINGS_MODULE。您应该可以导入您的模型,然后像Django一样正常使用,首先导入初始设置。



您不应该需要任何符号链接。 / p>

I have an existing Django application with a database and corresponding models.py file.

I have a new Tornado application that provides a web service to other applications. It needs to read/write from that same database, and there is code in the models file I'd like to use.

How can I best use the Django database and models in my Tornado request handlers? Is it as simple as making a symbolic link to the models.py Django project folder, importing Django modules, and using it? I guess I'd have to do settings.configure(), right?

Thanks!

解决方案

Add the path to the Django project to the Tornado application's PYTHONPATH env-var and set DJANGO_SETTINGS_MODULE appropriately. You should then be able to import your models and use then as normal with Django taking care of initial setup on the first import.

You shouldn't require any symlinks.

这篇关于如何在龙卷风应用程序中使用Django ORM?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆