Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6212195
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T06:28:18+00:00 2026-05-24T06:28:18+00:00

I have a django project with 2 apps like this: ## tags app, models.py

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T06:28:19+00:00Added an answer on May 24, 2026 at 6:28 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following Django and Flex code: Django class Author(models.Model): name = models.CharField(max_length=30)
I have a django project which is laid out like this... myproject apps media
I have worked a bit with Django and I quite like its project/applications model
If I have a Django form such as: class ContactForm(forms.Form): subject = forms.CharField(max_length=100) message
I stumbled over this passage in the Django tutorial : Django models have a
So in my Django project I have a few different apps, each with their
We have a Django project which runs on Google App Engine and used db.UserProperty
I have a django project which has two apps, one is AppA and AppB
I have successfully created my first django project. I have two apps in my
I have a Django project which has two apps (one created as debug test).

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.