在Django中,如何从主项目中查询一个单独的项目? [英] In django, how do I query a separate project from within my main project?

查看:41
本文介绍了在Django中,如何从主项目中查询一个单独的项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个项目(#1),其中有几个使用mysql数据库#1的应用程序.我还有另一个项目(#2),其django代码在同一服务器上,但是它使用了不同的mysql数据库(#2).

I have a project (#1) with a couple apps in it using a mysql database #1. I also have another project (#2) whose django code is on the same server, but it uses a different mysql database (#2).

我的目标是在项目#1中运行一个Web应用程序,并使用项目#2中的django模型针对数据库#2进行简单查询.但是,当我从项目#2导入时,它仍然使用#1数据库,即使#2的settings.py文件正确地使用数据库#2(即,当我在项目#2中运行Web应用程序时,它也可以正常工作).

My goal is to be running a web app in project #1 and use the django models from project #2 for a simple query against db #2. However, when I import from project #2, it still uses the #1 database, even though the settings.py file for #2 is appropriately using database #2 (i.e. when I run the web app in project #2, it works fine).

这是我可以作为独立脚本成功运行的文件的全部.可悲的是,当我将文件导入项目#1并运行该函数时,它失败了(因为它正在db#1中查找该表):

Here is the entirety of a file that I can successfully run as a standalone script. Sadly, when I import the file into project #1 and run the function, it fails (because it is looking in db#1 for the table):

import sys

def get_stuff_from_project2(ids):
    from django.core.management import setup_environ
    from project2 import settings
    setup_environ(settings)

    from project2.myapp2.models import mymodel2

    all_rows = mymodel2.objects.filter(id__in=ids).values()
    return(all_rows)

# as a standalone script, run the main function
if __name__ == "__main__":
    sys.path.append("/home/me/django")
    print str ( get_stuff_from_project2( sys.argv[1:] ) )

同样,这是一个独立的脚本.但是,在项目1(使用下面的代码)中,它失败并出现DatabaseError,表'db1.myapp2_mymodel2'不存在:

Again, this works as a standalone script. But, from project #1 (using code below) it fails with a DatabaseError, Table 'db1.myapp2_mymodel2' doesn't exist:

from project1.myapp1.standalone_script import get_stuff_from_project2
all_rows = get_stuff_from_project2( ids )

我的猜测是setup_environ函数实际上不会处理新的DATABASE_NAME,或者一旦设置完成就无法更改现有的DATABASE_NAME?

My guess here is that the setup_environ function does not actually process the new DATABASE_NAME, or that it can't change an existing DATABASE_NAME once the settings have been set?

这时我有点迷茫,一直在寻找解决方案.我真的不想放弃多站点"或多数据库"方法,因为我真的很想将项目1和项目2尽可能地分开.我的替代方法是从项目1中调用独立脚本作为系统调用,或者在项目2中创建一个视图,该视图是一个API,然后将数据发送出去.但是,我认为,只要可行,仅使用模型就最简单.

I'm a bit lost at this point and have been trying to search for a solution. I don't really want to go down the "multi-site" or "multi-database" approach, since I would really like to keep project 1 and project 2 as separate as possible. My alternative would be to call the standalone script as a system call from within project 1, or to make a view in project 2 which is an API and sends data out. But, I thought that just using a model would be simplest if it worked.

谢谢.

-------太平洋标准时间4月13日11:35添加----

------- added April 13, 11:35 PST ----

这是问题的一个简单版本:如何从一个独立脚本访问两个不同的项目.无论我首先执行setup_environ的哪个项目,以下代码都可以正常运行,但是第二个项目则无法执行:

Here's a simpler version of the question: How can I access two different projects from a single standalone script. The following code works ok for whichever project I do setup_environ on first, but it can't do the second one:

import sys
from django.core.management import setup_environ
sys.path.append('/home/me/django')

from project1 import settings
print setup_environ(settings)  # shows /home/me/django/project1
print settings.DATABASE_NAME   # shows db1
from project1.myapp1.models import mymodel1
mymodel1.objects.filter(id=9376544).values()  # works fine

from project2 import settings
print setup_environ(settings)  # shows /home/me/django/project2
print settings.DATABASE_NAME   # shows db2
from project2.myapp2.models import mymodel2
mymodel2.objects.filter(id=6544).values() # fails with:
# django.db.utils.DatabaseError: (1146, "Table 'db1.myapp2_mymodel2' doesn't exist")

推荐答案

我无法使用单个脚本解决此问题.相反,我使用了两个脚本:第一个称为第二个脚本,作为一个shell命令,第二个输出格式化的数据作为stdout的列表.

I was unable to solve this using a single script. I instead used two scripts: the first called the second as a shell command, and the second output formatted data as a list to stdout.

对于从另一个应用程序的数据库中读取单个数据流的问题,它已经足够健壮,但是不适用于较大的问题,例如,需要多次查询或写入第二个应用程序的数据.对于更复杂的问题,请添加API(在Django中,XMLRPC,REST很容易)来调用正在运行的应用程序.

This has been robust enough for the problem of reading a single stream of data from another app's database, but would not be suitable for a larger problem such as requiring several queries or writing to the second app's data. For the more complex problem, add APIs (XMLRPC, REST are easy in Django) to make calls into the running apps.

这篇关于在Django中,如何从主项目中查询一个单独的项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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