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

  • Home
  • SEARCH
  • 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 7778539
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T18:25:23+00:00 2026-06-01T18:25:23+00:00

This is a bit of a puzzle, I have these pseudo models: class Country(models.Model):

  • 0

This is a bit of a puzzle, I have these pseudo models:

class Country(models.Model):
   name = models.CharField(unique=True)

class Region(models.Model):
   name = models.CharField(unique=True)
   country = models.ForeignKey(Country)

class SubRegion(models.Model):
   name = models.CharField(unique=True)
   region = models.ForeignKey(Region)

class Estate(models.Model):
   name = models.CharField(unique=True)
   sub_region = models.ForeignKey(SubRegion)

I am trying to JSON serialize their data as below. However i’m not sure how to do this effectively (avoiding too much database queries), suggestions are appreciated

{

    CountryX: { 
        RegionX {
            SubRegionX = [
                "EstateX"
                "EstateY",
                "EstateZ"
            ],
            SubRegionY = [ etc... ]
        },
        RegionY { etc... }
    },
    CountryY: { etc... }

}
  • 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-06-01T18:25:25+00:00Added an answer on June 1, 2026 at 6:25 pm

    I haven’t tested this, but it should give you the idea. Start with the innermost object, use select_related to traverse the heirarchy, then loop over the innermost objects, adding the keys for the heirarchy as needed.

    Just a note of warning, if there are Countries/Regions/Subregions without any estates, they won’t be included in the JSON. If that’s not OK, you’ll need to query each of the models separately.

    data = {}
    for e in Estate.objects.select_related("sub_region__region__country"):
        sub, region, country = e.sub_region, e.sub_region.region, e.sub_region.region.country
        if country.name not in data:
            data[country.name] = {}
        if region.name not in data[country.name]:
            data[country.name][region.name] = {}
        if sub.name not in data[country.name][region.name]:
           data[country.name][region.name][sub.name] = []
        data[country.name][region.name][sub.name].append(e.name)
    json_data = json.dumps(data)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a bit of a puzzle - I have created a small game
Hey guys this is a bit of a homework puzzle I'm working on and
So I have this bit of code for x in range(x1,x2): for y in
I have this bit of code: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data;
I am a bit puzzled with the Django object models. I have models like
I have encountered this interview puzzle and want to know its accurate answer. You
I bit of a puzzle to solve in CF here. You have a timestamp
I have a bit of a puzzle (at least for me) which I am
I have a question regarding this database design. I am a bit unsure of
I've just stumbled across this and I'm a bit puzzled. I have an out-of-the-box

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.