title: Avoid Circular Model Imports in Django Apps
body: <p>I have a django project with 2 apps like this:</p>
  
  <pre><code>## tags app, models.py
  class Tag(models.Model):
      title = models.CharField(max_length=50)
  
  
  ## items app, models.py
  from application.tags.models import Tag
  
  class Item(models.Model):
      title = models.CharField(max_length=300)
      tags = models.ManyToManyField(Tag, related_name="items")
  </code></pre>
  
  <p><strong>UPDATE TO CLARIFY FUNCTION LOCATION</strong></p>
  
  <p>I have a method on another model in <code>items.models</code> which gets all of the items that have a set of tags. </p>
  
  <p>The resulting query is like:</p>
  
  <pre><code>## Gets all of the items that have tags t1 and t2
  Item.objects.filter(tags=t1).filter(tags=t2)
  </code></pre>
  
  <p>This method uses the <code>Item</code> model as well as the <code>Tag</code> model, which is ok, since Tag is imported into the items app.</p>
  
  <p>However, I want to have access to this method in the tags app, but doing so would result in a circular import.</p>
  
  <p>Right now my workaround to get all of the items with a set of tags in the tags app is to do a set intersection on the reverse relation in the many to many field.</p>
  
  <pre><code>## Get all items that have the tags with ids tag_ids
  item_set = set(Tag.objects.get(pk=tag_ids[0]).items.all())
  for cur_tag_id in tag_ids[1:]: ## for all of the rest of the tags
       item_set = item_set &amp; set(Tag.objects.get(pk=cur_tag_id).items.all())
  </code></pre>
  
  <p>This results in several more queries and a set intersection. Is there a way I can do something like <code>Item.objects.filter(tags=t1).filter(tags=t2)...</code> from the Tag model in the tags app?</p>
  
  <p>I was able to get around this using <code>contenttypes</code> to get the <code>Item</code> model to make the same query. Is this acceptable, or is there a better way to organize this code?</p>