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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T00:58:45+00:00 2026-06-01T00:58:45+00:00

Lets say we have the following models: (simplified) class Collection(models.Model): slug = models.SlugField() class

  • 0

Lets say we have the following models: (simplified)

class Collection(models.Model):
    slug = models.SlugField()

class Brochure(models.Model):
    collection = models.ForeignKey(Collection)

class Page(models.Model):
    brochures = models.ManyToMany(Brochure)
  • There are 2 collections ‘x’ and ‘y’.
  • A page has a selection of brochures, not all.

Is there a way to get a dictionary of brochures like this:

{'x': [brochure1, brochure2], 'y': [brochure3, brochure4]}

only by using the many-to-many manager:

page_instance.brochures.all()

EDIT

I want to work with the brochures related to Page. Not Brochures.objects.all() or collection_instance.brochure_set. That’s why I included Page.

I found defaultdict, which helps me do this:

collection_brochures = defaultdict(list)
for b in self.brochures.all():
    collection_brochures[b.collection.slug].append(b)

Actually this is the answer to my question.

I can work with this, but ideal would be a list with dicts like this:

[{'collection': 'x', 'brochures': [brochure1, brochure2]}, {'collection': 'y', 'brochures': [brochure3, brochure4]}]
  • 1 1 Answer
  • 1 View
  • 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-06-01T00:58:46+00:00Added an answer on June 1, 2026 at 12:58 am

    I hope this code snippet will do the job.

    collection_brochures = defaultdict(list)
    for b in self.brochures.all():
        collection_brochures[b.collection.slug].append(b)
    result = []
    for collection, brochures in collection_brochures.iteritems():
        result.append({'collection': collection, 'brochures': brochures})
    

    Also you can use model instances as keys, so if you wish to see Collection instances instead of their slugs, you may replace third line with this one:

    collection_brochures[b.collection].append(b)
    

    This will give you following data:

    [{'collection': collection_x, 'brochures': [brochure1, brochure2]},
     {'collection': collection_y, 'brochures': [brochure3, brochure4]}]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have the following Django model: class Info(models.Model): instrument = models.ForeignKey('Instrument') date
Lets say a have the following Model public class FirstModel { public List<SecondModel> SecondModels
Let's say I have the following Django models: class A(models.Model): keywords = models.ManyToManyField(Keyword) class
Let's say I have the following: class Employee(models.Model): firstName = models.CharField(max_length = 30) lastName
Let's say I have the following model: class Contest: title = models.CharField( max_length =
Let's say I have following ORM classes (fields removed to simplify): class Animal(models.Model): say
Let's say I have the following models class Photo(models.Model): tags = models.ManyToManyField(Tag) class Tag(models.Model):
Lets say, I have following models in my Django app. class EventGroup name =
Let's say I have the following classes: class Votable(models.Model): name = ... class Vote(models.Model):
Lets say i have the following c# classes: abstract class a { protected abstract

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.