title: Django ImportError: cannot import name 'x'
body: <p>I have problems with circularity in my two files. Model import function to run when create object and this function import model to check if code in unique.</p>
  
  <p>How use model in function and function in model without problem with circularity? I checked questions simillar to my problem but i still don't know to fix this issue.</p>
  
  <p><strong>models.py</strong></p>
  
  <pre><code>from django.contrib.auth.models import User
  from django.core.urlresolvers import reverse
  from django.db import models
  from .middleware.current_user import get_current_user
  from shortener.utils import create_shortcode
  from django.conf import settings
  CODE_MAX_LENGTH = getattr(settings, 'CODE_MAX_LENGTH', 16)
  
  
  class Shortener(models.Model):
      url = models.URLField()
      code = models.CharField(unique=True, blank=True, max_length=CODE_MAX_LENGTH)
      author = models.ForeignKey(User, blank=True, null=True)  # Allow anonymous
      created = models.DateTimeField(auto_now_add=True)
      modified = models.DateTimeField(auto_now=True)
      active = models.BooleanField(default=True)
  
      def save(self, *args, **kwargs):
          if not self.pk:
              self.author = get_current_user()
  
              if self.code in [None, ""]:
                  self.code = create_shortcode()
              elif self.code.find(' ') != -1:
                  self.code = self.code.replace(' ', '_')
  
              if self.url not in ["http", "https"]:
                  self.url = "http://{0}".format(self.url)
  
          super(Shortener, self).save(*args, **kwargs)
  
      def __str__(self):
          return self.url
  
      def __unicode__(self):
          return self.url
  
      def get_short_url(self):
          return reverse("redirect", kwargs={'code': self.code})
  </code></pre>
  
  <p><strong>Utils.py</strong></p>
  
  <pre><code>import random
  import string
  from django.conf import settings
  from shortener.models import Shortener
  SIZE = getattr(settings, 'CODE_GENERATOR_MAX_SIZE', 12)
  
  
  def code_generator(size=SIZE):
      return ''.join(random.choice(string.ascii_lowercase + string.ascii_uppercase + string.digits) for _ in range(size))
  
  
  def create_shortcode():
      code = code_generator()
  
      if Shortener.objects.filter(code=code).exists():
          create_shortcode()
  
      return code
  </code></pre>
  
  <p><strong>Traceback:</strong></p>
  
  <pre><code>Unhandled exception in thread started by &lt;function check_errors.&lt;locals&gt;.wrapper at 0x037EAB28&gt;
  Traceback (most recent call last):
    File "C:\Users\loc\shortener\lib\site-packages\django\utils\autoreload.py", line 226, in wrapper
      fn(*args, **kwargs)
    File "C:\Users\loc\shortener\lib\site-packages\django\core\management\commands\runserver.py", line 113, in inner_run
      autoreload.raise_last_exception()
    File "C:\Users\loc\shortener\lib\site-packages\django\utils\autoreload.py", line 249, in raise_last_exception
      six.reraise(*_exception)
    File "C:\Users\loc\shortener\lib\site-packages\django\utils\six.py", line 685, in reraise
      raise value.with_traceback(tb)
    File "C:\Users\loc\shortener\lib\site-packages\django\utils\autoreload.py", line 226, in wrapper
      fn(*args, **kwargs)
    File "C:\Users\loc\shortener\lib\site-packages\django\__init__.py", line 27, in setup
      apps.populate(settings.INSTALLED_APPS)
    File "C:\Users\loc\shortener\lib\site-packages\django\apps\registry.py", line 108, in populate
      app_config.import_models(all_models)
    File "C:\Users\loc\shortener\lib\site-packages\django\apps\config.py", line 199, in import_models
      self.models_module = import_module(models_module_name)
    File "E:\Python\Python35-32\lib\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 665, in exec_module
    File "&lt;frozen importlib._bootstrap&gt;", line 222, in _call_with_frames_removed
    File "C:\Users\loc\PycharmProjects\DjangoURLShortener\shortener\models.py", line 4, in &lt;module&gt;
      from shortener.utils import create_shortcode
    File "C:\Users\loc\PycharmProjects\DjangoURLShortener\shortener\utils.py", line 4, in &lt;module&gt;
      from shortener.models import Shortener
  ImportError: cannot import name 'Shortener'
  </code></pre>