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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:52:23+00:00 2026-05-27T09:52:23+00:00

I’m trying to do something like this: class A(models.Model): members = models.ManyToManyField(B) class B(models.Model):

  • 0

I’m trying to do something like this:

class A(models.Model):
    members = models.ManyToManyField(B)

class B(models.Model):
    pass

# not sure what the right query is here
results = A.objects.all().[members.extra(select={"extra_field": ...})?]

# I want to be able to write this using the result of my query:
for r in result:
    for m in r.members:
        print m.extra_field

Is it possible to populate this extra_field without creating a new query for every model B in members?

  • 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-27T09:52:24+00:00Added an answer on May 27, 2026 at 9:52 am

    Until prefetching arrives in Django 1.4, this will get a little hairy. Hold onto your hat.

    First, you need to be able to query over the table that links your two models. If you don’t want to explicitly define a through table, you can use an unmanaged model like so:

    class AB(models.Model):
        a = models.ForeignKey('A')
        b = models.ForeignKey('B')
    
        class Meta:
            db_table = 'myapp_a_b'
            managed = False
    

    Once you have this you can take a deep breath, hold your nose and do something like the following:

    # Grab the links between A and B
    a_b_relationships = AB.objects.all().values('a_id', 'b_id')
    
    # Make an indexed reference for your B objects
    all_b = B.objects.all().extra(select={'extra_field': ...})
    b_dict = dict((b.pk, b) for b in all_b)
    
    # Make a dict so that for any given A object we can immediately grab
    # a list of its B objects
    b_by_a = {}
    for rel in a_b_relationships:
        a_id = rel['a_id']
        b = b_dict[rel['b_id']]
    
        if a_id not in b_by_a:
            b_by_a[a_id] = []
    
        if b not in b_by_a[a_id]
            b_by_a[a_id].append(b)
    
    results = A.objects.all()
    
    for r in result:
        members = b_by_a.get(r.id, [])
    
        for m in members:
            print m.extra_field
    

    It’s nasty, but it works. Keep in mind that if the A and B tables start to get big then you’re going to run into performance issues – Django objects take up a lot of memory and can be very slow to iterate over. If you end up filtering or chunking A you’ll have to add the appropriate filters to the AB and B queries as well.

    If anyone has a cleaner/more efficient way of doing this, I’d love to know!

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have some data like this: 1 2 3 4 5 9 2 6
Basically, what I'm trying to create is a page of div tags, each has
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I would like to count the length of a string with PHP. The string

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.