Avoid Circular Model Imports in Django Apps
Asked Answered
I

1

12

I have a django project with 2 apps like this:

## 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")

UPDATE TO CLARIFY FUNCTION LOCATION

I have a method on another model in items.models which gets all of the items that have a set of tags.

The resulting query is like:

## Gets all of the items that have tags t1 and t2
Item.objects.filter(tags=t1).filter(tags=t2)

This method uses the Item model as well as the Tag model, which is ok, since Tag is imported into the items app.

However, I want to have access to this method in the tags app, but doing so would result in a circular import.

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.

## 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 & set(Tag.objects.get(pk=cur_tag_id).items.all())

This results in several more queries and a set intersection. Is there a way I can do something like Item.objects.filter(tags=t1).filter(tags=t2)... from the Tag model in the tags app?

I was able to get around this using contenttypes to get the Item model to make the same query. Is this acceptable, or is there a better way to organize this code?

Inventive answered 3/8, 2011 at 7:29 Comment(2)
You haven't really given all the relevant information. Where is your filter method defined? Why can't you put it somewhere that can be imported by both apps?Alternative
That's the problem I am trying to fix. It is a method on a different model in the items app. The problem is items.models imports Tag, and if this method is is in another place, it would still need to import Item and Tag. Then Tag could not import it or it would still be circular.Inventive
S
28

When you define models with foreign keys, you can use the format:

tags = models.ManyToManyField('tags.Tag', ...)

This means you don't need to have imported the Tag class, just installed the tags app.

Then you can store a function in a different location, that might import both Tag and Item, and not have to worry about circular imports.

Spahi answered 3/8, 2011 at 8:9 Comment(6)
Thanks. This helps, but the issue is if I want to use that function from a different location in the models for both apps, then I still get circular imports.Inventive
You can use django.db.models.get_model('app.Model') to get a reference to a model without having to import it. Does that help?Spahi
That does it! I needed to do get_model('app', 'Model'), but that worked. Is it bad practice to retrieve models this way?Inventive
Adding, as a reference, the documentation for this, which is called a recursive relationship: docs.djangoproject.com/en/dev/ref/models/fields/…Megrim
Erm. I'm not sure that this is a recursive relationship. It's pointing to a different model.Spahi
get_model has moved: #36235135Halliehallman

© 2022 - 2024 — McMap. All rights reserved.