title: Connecting Models from different Apps - Django
body: <p>Given that I have 4 models:</p>
  
  <ul>
  <li><code>Project</code></li>
  <li><code>User</code> (from django.contrib.auth.models)</li>
  <li><code>Sentence</code></li>
  <li><code>Translation</code></li>
  </ul>
  
  <p>My django project directories are structured as such:</p>
  
  <pre><code> \myprojname
       \project
           models.py
       \data
           models.py
  </code></pre>
  
  <p>The <code>User</code> model is independent of the <code>Projects</code> and <code>Sentences</code> models but it is linked to the <code>Projects</code> model through the ForeignKey:</p>
  
  <p><code>myprojname/projects/models.py</code> looks like this:</p>
  
  <pre><code>from django.db import models
  from django.contrib.auth.models import User
  
  class Project(models.Model):
      project_id = models.AutoField(primary_key=True)
      created_on = models.DateTimeField(auto_now_add=True)
      updated_on = models.DateTimeField(auto_now=True)
  
      name = models.CharField(max_length=140)
      # Admin user for the particular *Project* object.
      # Have the rights to write to the *Sentence* model.
      owner = models.ForeignKey(User)
      # Worker users that only have write access to the *Translation model.
      workers = models.ForeignKey(User, blank=True)
  </code></pre>
  
  <p><code>myprojname/data/models.py</code> looks like this:</p>
  
  <pre><code>from django.db import models
  from django.contrib.auth.models import User
  
  class Sentence(models.Model):
      text = models.TextField()
      translations = models.ManyToManyField('Translation', blank=True)
  
  class Translation(models.Model):
      text = models.TextField()
      translator = models.ForeignKey(User)
  </code></pre>
  
  <p>Now the <code>Project</code> model isn't connected to the <code>Sentence</code> model. </p>
  
  <p>I've tried connecting it with relative imports, i.e. in <code>myprojname/projects/models.py</code>, I did:</p>
  
  <pre><code>from django.db import models
  from django.contrib.auth.models import User
  from ..data import Sentence
  
  class Project(models.Model):
      # ...
      sentences = models.ForeignKey(Sentence)
  </code></pre>
  
  <p>but it returned a <code>ValueError</code> in <code>Python2.7</code>: </p>
  
  <pre><code>Traceback (most recent call last):
    File "/usr/local/lib/python2.7/dist-packages/django/utils/autoreload.py", line 226, in wrapper
      fn(*args, **kwargs)
    File "/usr/local/lib/python2.7/dist-packages/django/core/management/commands/runserver.py", line 109, in inner_run
      autoreload.raise_last_exception()
    File "/usr/local/lib/python2.7/dist-packages/django/utils/autoreload.py", line 249, in raise_last_exception
      six.reraise(*_exception)
    File "/usr/local/lib/python2.7/dist-packages/django/utils/autoreload.py", line 226, in wrapper
  
      fn(*args, **kwargs)
    File "/usr/local/lib/python2.7/dist-packages/django/__init__.py", line 18, in setup
      apps.populate(settings.INSTALLED_APPS)
    File "/usr/local/lib/python2.7/dist-packages/django/apps/registry.py", line 108, in populate
      app_config.import_models(all_models)
    File "/usr/local/lib/python2.7/dist-packages/django/apps/config.py", line 202, in import_models
      self.models_module = import_module(models_module_name)
    File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
      __import__(name)
    File "/home/alvas/data-project/myprojname/project/models.py", line 5, in &lt;module&gt;
      from ..data import Sentence
  ValueError: Attempted relative import beyond toplevel package
  </code></pre>
  
  <p>And in <code>Python3</code>, the same error:</p>
  
  <pre><code>Traceback (most recent call last):
    File "/home/alvas/.virtualenvs/payer/lib/python3.5/site-packages/django/utils/autoreload.py", line 226, in wrapper
      fn(*args, **kwargs)
    File "/home/alvas/.virtualenvs/payer/lib/python3.5/site-packages/django/core/management/commands/runserver.py", line 109, in inner_run
      autoreload.raise_last_exception()
    File "/home/alvas/.virtualenvs/payer/lib/python3.5/site-packages/django/utils/autoreload.py", line 249, in raise_last_exception
      six.reraise(*_exception)
    File "/home/alvas/.virtualenvs/payer/lib/python3.5/site-packages/django/utils/six.py", line 685, in reraise
  
      raise value.with_traceback(tb)
    File "/home/alvas/.virtualenvs/payer/lib/python3.5/site-packages/django/utils/autoreload.py", line 226, in wrapper
      fn(*args, **kwargs)
    File "/home/alvas/.virtualenvs/payer/lib/python3.5/site-packages/django/__init__.py", line 18, in setup
      apps.populate(settings.INSTALLED_APPS)
    File "/home/alvas/.virtualenvs/payer/lib/python3.5/site-packages/django/apps/registry.py", line 108, in populate
      app_config.import_models(all_models)
    File "/home/alvas/.virtualenvs/payer/lib/python3.5/site-packages/django/apps/config.py", line 202, in import_models
      self.models_module = import_module(models_module_name)
    File "/home/alvas/.virtualenvs/payer/lib/python3.5/importlib/__init__.py", line 126, in import_module
      return _bootstrap._gcd_import(name[level:], package, level)
    File "&lt;frozen importlib._bootstrap&gt;", line 986, in _gcd_import
    File "&lt;frozen importlib._bootstrap&gt;", line 969, in _find_and_load
    File "&lt;frozen importlib._bootstrap&gt;", line 958, in _find_and_load_unlocked
    File "&lt;frozen importlib._bootstrap&gt;", line 673, in _load_unlocked
    File "&lt;frozen importlib._bootstrap_external&gt;", line 662, in exec_module
    File "&lt;frozen importlib._bootstrap&gt;", line 222, in _call_with_frames_removed
    File "/home/alvas/data-project/myprojname/project/models.py", line 5, in &lt;module&gt;
      from ..data import Sentence
  ValueError: attempted relative import beyond top-level package
  </code></pre>
  
  <p>And when I tried to do a non-relative import in <code>Python3</code>:</p>
  
  <pre><code>from django.db import models
  from django.contrib.auth.models import User
  from myprojname.data import Sentence
  
  class Project(models.Model):
      # ...
      sentences = models.ForeignKey(Sentence)
  </code></pre>
  
  <p>I get:</p>
  
  <pre><code>Traceback (most recent call last):
    File "/home/alvas/.virtualenvs/payer/lib/python3.5/site-packages/django/utils/autoreload.py", line 226, in wrapper
      fn(*args, **kwargs)
    File "/home/alvas/.virtualenvs/payer/lib/python3.5/site-packages/django/core/management/commands/runserver.py", line 109, in inner_run
  
      autoreload.raise_last_exception()
    File "/home/alvas/.virtualenvs/payer/lib/python3.5/site-packages/django/utils/autoreload.py", line 249, in raise_last_exception
      six.reraise(*_exception)
    File "/home/alvas/.virtualenvs/payer/lib/python3.5/site-packages/django/utils/six.py", line 685, in reraise
      raise value.with_traceback(tb)
    File "/home/alvas/.virtualenvs/payer/lib/python3.5/site-packages/django/utils/autoreload.py", line 226, in wrapper
      fn(*args, **kwargs)
    File "/home/alvas/.virtualenvs/payer/lib/python3.5/site-packages/django/__init__.py", line 18, in setup
      apps.populate(settings.INSTALLED_APPS)
    File "/home/alvas/.virtualenvs/payer/lib/python3.5/site-packages/django/apps/registry.py", line 108, in populate
      app_config.import_models(all_models)
    File "/home/alvas/.virtualenvs/payer/lib/python3.5/site-packages/django/apps/config.py", line 202, in import_models
      self.models_module = import_module(models_module_name)
    File "/home/alvas/.virtualenvs/payer/lib/python3.5/importlib/__init__.py", line 126, in import_module
      return _bootstrap._gcd_import(name[level:], package, level)
    File "&lt;frozen importlib._bootstrap&gt;", line 986, in _gcd_import
    File "&lt;frozen importlib._bootstrap&gt;", line 969, in _find_and_load
    File "&lt;frozen importlib._bootstrap&gt;", line 958, in _find_and_load_unlocked
    File "&lt;frozen importlib._bootstrap&gt;", line 673, in _load_unlocked
    File "&lt;frozen importlib._bootstrap_external&gt;", line 662, in exec_module
    File "&lt;frozen importlib._bootstrap&gt;", line 222, in _call_with_frames_removed
    File "/home/alvas/data-project/myprojname/project/models.py", line 5, in &lt;module&gt;
      from myprojname.data import Sentence
  ImportError: No module named 'myprojname.data'
  </code></pre>
  
  <p><strong>How do I connect the models from different apps?</strong></p>
  
  <hr>
  
  <h1>Edited</h1>
  
  <p>When I used <code>from data import Sentence</code> in <code>myprojname.project.models.py</code>, it throws an <code>ImportError</code>:</p>
  
  <pre><code>Traceback (most recent call last):
    File "/home/alvas/.virtualenvs/statnlp/lib/python3.5/site-packages/django/utils/autoreload.py", line 226, in wrapper
      fn(*args, **kwargs)
    File "/home/alvas/.virtualenvs/statnlp/lib/python3.5/site-packages/django/core/management/commands/runserver.py", line 109, in inner_run
      autoreload.raise_last_exception()
    File "/home/alvas/.virtualenvs/statnlp/lib/python3.5/site-packages/django/utils/autoreload.py", line 249, in raise_last_exception
      six.reraise(*_exception)
    File "/home/alvas/.virtualenvs/statnlp/lib/python3.5/site-packages/django/utils/six.py", line 685, in reraise
      raise value.with_traceback(tb)
    File "/home/alvas/.virtualenvs/statnlp/lib/python3.5/site-packages/django/utils/autoreload.py", line 226, in wrapper
      fn(*args, **kwargs)
  
    File "/home/alvas/.virtualenvs/statnlp/lib/python3.5/site-packages/django/__init__.py", line 18, in setup
      apps.populate(settings.INSTALLED_APPS)
    File "/home/alvas/.virtualenvs/statnlp/lib/python3.5/site-packages/django/apps/registry.py", line 108, in populate
      app_config.import_models(all_models)
    File "/home/alvas/.virtualenvs/statnlp/lib/python3.5/site-packages/django/apps/config.py", line 202, in import_models
      self.models_module = import_module(models_module_name)
    File "/home/alvas/.virtualenvs/statnlp/lib/python3.5/importlib/__init__.py", line 126, in import_module
      return _bootstrap._gcd_import(name[level:], package, level)
    File "&lt;frozen importlib._bootstrap&gt;", line 986, in _gcd_import
    File "&lt;frozen importlib._bootstrap&gt;", line 969, in _find_and_load
    File "&lt;frozen importlib._bootstrap&gt;", line 958, in _find_and_load_unlocked
    File "&lt;frozen importlib._bootstrap&gt;", line 673, in _load_unlocked
    File "&lt;frozen importlib._bootstrap_external&gt;", line 662, in exec_module
    File "&lt;frozen importlib._bootstrap&gt;", line 222, in _call_with_frames_removed
    File "/home/alvas/data-project/myprojname/project/models.py", line 5, in &lt;module&gt;
      from data import Sentence
  ImportError: cannot import name 'Sentence'
  </code></pre>