title: In django, how do I query a separate project from within my main project?
body: <p>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).</p>
  
  <p>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).</p>
  
  <p>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):</p>
  
  <pre><code>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:] ) )
  </code></pre>
  
  <p>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:</p>
  
  <pre><code>from project1.myapp1.standalone_script import get_stuff_from_project2
  all_rows = get_stuff_from_project2( ids )
  </code></pre>
  
  <p>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?</p>
  
  <p>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.</p>
  
  <p>Thanks.</p>
  
  <p>-------  added April 13, 11:35 PST ----</p>
  
  <p>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:</p>
  
  <pre><code>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")
  </code></pre>