title: How to use Django models outside of Django?
body: <p>Following <a href="https://stackoverflow.com/questions/33170016/how-to-use-django-1-8-5-orm-without-creating-a-django-project">this</a> guide, I am able to use models outside of Django with the following file structure by calling <code>python main.py</code>.</p>
  
  <pre><code>├── data
  │   ├── __init__.py
  │   ├── migrations
  │   │   └── __init__.py
  │   └── models.py
  ├── main.py
  ├── manage.py
  └── settings.py
  </code></pre>
  
  <p>where main.py looks like this:</p>
  
  <pre><code>import os, django
  os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'settings')
  
  django.setup()
  from data.models import Foo, Bar #...
  
  print(Foo.objects.all()) #this works fine
  </code></pre>
  
  <p>What I want to do is turn this into a "package" called <code>db</code> that looks like this:</p>
  
  <pre><code>    ├── data
      │   ├── __init__.py
      │   ├── migrations
      │   │   └── __init__.py
      │   └── models.py
      ├── __init__.py 
      ├── manage.py
      └── settings.py
  </code></pre>
  
  <p>And in the <code>__init__.py</code> of the <code>db</code> package, I want to do this:</p>
  
  <pre><code>import os, django
  os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'settings')
  
  django.setup()
  from data.models import Foo, Bar # ...
  from django.db import connection
  
  __all__ = [
      'connection',
      'Foo',
      'Bar', 
      #...
  ]
  </code></pre>
  
  <p>So I can call the <code>db</code> package from <code>test.py</code> (which is in the same directory as <code>db</code>) like this:</p>
  
  <pre><code>import db
  
  print(db.Foo.objects.all()) #this throws an error "no module named data"
  </code></pre>
  
  <p>or like this:</p>
  
  <pre><code>from db import Foo
  
  print(Foo.objects.all()) # this throws an error "no module named settings"
  </code></pre>
  
  <p>Is there a way I can use Django's models without having to call:
  <code>os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'settings')</code>
  <code>django.setup()</code></p>
  
  <p>on every page that uses a model?</p>
  
  <p>What am I missing here?</p>