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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:51:36+00:00 2026-05-26T12:51:36+00:00

There are following models: class Schema(models.Model): keys = models.ManyToManyField(Key, through=’SchemaHasKey’) # … class Key(models.Model):

  • 0

There are following models:

class Schema(models.Model):
    keys = models.ManyToManyField(Key, through='SchemaHasKey')
    # ...

class Key(models.Model):
    name   = models.CharField(max_length=50)
    # ...

I need select 10 first schemas with the keys, but my way is bad:

schemas = []

for schema in Schema.objects.all().order_by('pk')[:10]:
    schema.key = schema.keys.all()
    schemas.append(schema)

.select_related() is not work well:

schemas = Schema.objects.select_related().order_by('pk')[:10]

This can be done in a certain number of sql queries?

  • 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-26T12:51:36+00:00Added an answer on May 26, 2026 at 12:51 pm

    Here’s how I might do it until prefetch_related functionality is officially launched.

    2 queries.

    from collections import defaultdict
    
    schemas = Schema.objects.order_by('pk')[:10]
    key_map = defaultdict(lambda:[]) # always return a list
    
    # use m2m through table to get all schema-key relationships related to the 10
    # create a list of each `Key` with the Schema ID as the dict key.
    [key_map[through.schema.id].append(through.key) for 
        through in Schema.keys.through.objects.filter(schema__in=schemas)]
    
    for schema in schemas:
        schema.keys = key_map[schema.id]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following models: class Indicator(models.Model): name = models.CharField(max_length=200) category = models.ForeignKey(IndicatorCategory) weight
For the following models: class Entity(models.Model): name = models.CharField(max_length=256) class Entry(models.Model): A <subj> has
I have the following models: class Category(models.Model): name = models.CharField(max_length=40) class Item(models.Model): name =
I defined two models: class Server(models.Model): owners = models.ManyToManyField('Person') class Person(models.Model): name = models.CharField(max_length=50)
I have the following models: class Order_type(models.Model): description = models.CharField() class Order(models.Model): type= models.ForeignKey(Order_type)
I have the following models: class A(TranslatableModel): translations = TranslatedFields( name = models.CharField(max_length=30) )
Given the following models:(don't mind the TextFields there're just for illustration) class Base(models.Model): field1
I have the following models: class Foo(models.Model): field1 = models.IntegerField() ... class Bar(models.Model): field1
Let's assume that I have following models: class ScoutBook(models.Model): troop = models.ForeignKey('Dictionary', limit_choices_to={'type' :
I have the following models: class Bill(models.Model): date = models.DateTimeField(_(Date of bill),null=True,blank=True) class Item(models.Model):

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.