比如我有这样一个项目,目录结构是这样的
我有一个需求,需要在外部引用blog下的models文件 然后做一些操作 ( 用django的ORM,而不是链接数据库来操作 )
那么要如何操作呢?
我发现直接引用是不行的!
换句话说 我想在外部直接使用django的orm~~~
比如我在项目中的根目录中的post
文件直接使用引用blog
中models的文件
PHP中文网2017-04-18 09:53:33
In fact, if you just want to use ORM to replace direct database operations, then I recommend using itsqlalchemy
,因为Django的ORM依赖与Django框架,但是Python里面最好用使用量最多的的还是sqlalchemy
天蓬老师2017-04-18 09:53:33
First make sure your blog directory is under the system path, then:
import django
django.setup()
from blog.models import <你要用到的models>
# 后面正常使用即可