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 6046769
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:16:40+00:00 2026-05-23T07:16:40+00:00

I have an ugly loop that is operating as a band-aid in Django that

  • 0

I have an ugly loop that is operating as a band-aid in Django that I’d like to optimize into a join. I’ve broken it down to the most basic structure to help simplify the problem. I’m hoping someone else has run into something like this before — if not, I’ll share the answer when I finally fix it.

To summarize

  • Object A has a non-symmetrical, many-to-many relationship with itself.
  • Object B has a many-to-one relationship with Object A.
  • Given an ObjectB, I need a set of other ObjectBs that are the children of the ObjectAs that are associated with our ObjectB’s parent ObjectA.

I’m sure someone with more database experience could phrase that better (leave a comment if you have a better description of this association).

Here is a bare bones example of the structure I’m working with in Django, and the sort of loop that’s running:

class ObjectA(models.Model):
    object_a_rules = models.ManyToManyField("self", symmetrical=False, through='ObjectARule')

class ObjectARule(models.Model):
    object_a_one = models.ForeignKey(ObjectA, related_name="object_a_before")
    object_a_two = models.ForeignKey(ObjectA, related_name="object_a_after")

class ObjectB(models.Model):
    object_a_association = models.ForeignKey(ObjectA)

    def that_annoying_loop_i_mentioned(self):
        object_a_rules_list = ObjectARule.objects.filter(object_a_one = self.object_a_association)
        #A list of all of the ObjectARules that have the ObjectA this ObjectB is associated with
        #as the first half of the many-to-many relationship.

        object_b_list = ObjectB.objects.all()
        #A list of all of the ObjectBs, may also be a filtered list

        for object_a_rule in object_a_rules_list:
            for object_b in object_b_list:
                if (object_a_rule.object_a_two == object_b.object_a_association):
                #if the second half of ObjectARule is the ObjectA of
                #the ObjectB in this list, then do something with that ObjectB.
                pass

How could Django get a list of ObjectBs through a join, so this painfully inefficient loop wouldn’t have to run?

  • 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-23T07:16:41+00:00Added an answer on May 23, 2026 at 7:16 am

    Given an ObjectB, I need a set of
    other ObjectBs that are the children
    of the ObjectAs that are associated
    with our ObjectB’s parent ObjectA.

    If objb is the ObjectB you are given, you could do this as follows:

    objects = ObjectB.objects.filter(object_a_association__object_a_rules=objb.object_a_association)
    

    or alternatively,

    objects = ObjectB.objects.filter(object_a_association__object_a_rules__objectb_set=objb)
    

    See also Lookups that span relationships

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

Sidebar

Related Questions

I have an ugly druple-gardens RSS feed coming into flash and I'm using the
I have the following ugly if statement that is part of a class that
I have a collection of objects that I would like to iterate through to
I have an network application which uses a select loop like this: bool shutdown=false;
I have a loop where I put a lot of entities into the datastore,
I have a code that looks something like this: struct First { int f1;
In my loop I am trying to remove items from a list that have
I have a file like this, that needs to be parsed and needs to
I have this ugly SELECT statement : Select 'Urbain' as Véhicule, Count( DISTINCT Plaintes.IDEvenements)
So have rewritten my ugly php URL to something prettier: RewriteEngine On RewriteRule ^main/photo([^/\.]+)/?.html$

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.