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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:38:59+00:00 2026-05-26T09:38:59+00:00

My data model is simple: class Neighborhood(models.Model): name = models.CharField(max_length = 50) slug =

  • 0

My data model is simple:

class Neighborhood(models.Model):
    name = models.CharField(max_length = 50)
    slug = models.SlugField()

class Location(models.Model):
    company = models.ForeignKey(Company)
    alt_name = models.CharField()
    neighborhoods = models.ManyToManyField(Neighborhood)

I would like to supply a page on my site that lists all locations by their neighborhood (s). If it were singular, I think {% regroup %} with a {% ifchanged %} applied to the neighborhood name would be all that I need, but in my case, having it be a m2m, I’m not sure how do this. A location may have multiple neighborhoods, and so I would like them to be redundantly displayed under each matching neighborhood.

I’m also aware of FOO_set but that’s per Object; I want to load the entire data set.

The final result (in the template) should be something like:

Alameda
    Crazy Thai
Castro
    Kellys Burgers
    Pizza Orgasmica
Filmore
    Kellys Burgers
    Some Jazz Bar
Mission
    Crazy Thai
    Elixir
...

The template syntax would (ideally?) look something like:

{% for neighborhood in neighborhood_list %}
    {% ifchanged %}{{ neighborhood.name }}{% endifchanged %}
    {% for location in neighborhood.restaurants.all %}
        {{ location.name }}
    {% endfor %}
{% endfor %}
  • 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-26T09:39:00+00:00Added an answer on May 26, 2026 at 9:39 am

    I’d just do it the expensive way and cache the result over scratching my head. Your template example would work fine until performance becomes an issue in generating that one page per X cache timeout.

    You could do it in python as well if the result set is small enough:

    # untested code - don't have your models
    from collections import defaultdict
    
    results = defaultdict(list)
    for location_m2m in Location.neighborhoods.through.objects.all() \ # line wrap
            .select_related('neighborhood', 'location', 'location__company__name'):
    
        if location_m2m.location not in results[location_m2m.neighborhood]:
            results[location_m2m.neighborhood].append(location_m2m.location)
    
    # sort now unique locations per neighborhood
    map(lambda x: x.sort(key=lambda x: x.location.company.name), results.values())
    
    # sort neighborhoods by name
    sorted_results = sorted(results.items(), key=lambda x:x[0].name)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple model: class MediaLink(models.Model): title = models.CharField(max_length=200) subtitle = models.TextField(max_length=2000, unique=False,
Is Model only Entity Data Model class of my database? Model as simple place
I have very simple data model like this: create table Company ( id int
I have a simple data model of two tables, email and recipients, email can
My application has a simple data model with 70 read-only records grouped into six
I'd like to serialize some simple data model into xml, I've been using the
Simple. I created a LINQ-TO-SQL Entity model, created a website, added a Data Service
I'm writing a small program to record reading progress, the data models are simple:
This is what my data model classes look like: public class Employee { public
I have a rather simple data model, but I have a centric entity called

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.