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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T00:58:48+00:00 2026-05-13T00:58:48+00:00

I have an app that’s about presenting fictional simplified cities. Please consider the following

  • 0

I have an app that’s about presenting fictional simplified cities.

Please consider the following Django models:

class City(models.Model):
    name = models.CharField(...)
    ...

TYPEGROUP_CHOICES = (
    (1, 'basic'),
    (2, 'extra'),
)

class BldgType(models.Model):
    name = models.CharField(...)
    group = models.IntegerField(choices=TYPEGROUP_CHOICES)

class Building(models.Model):
    created_at = models.DateTimeField(...)
    city = models.ForeignKey(City)
    type = models.ForeignKey(BldgType)
    other_criterion = models.ForeignKey(...)

    class Meta:
        get_latest_by = 'created_at'

Explanations for choosing this setup:

(1) Each city has certain buildings of a “basic” type which occur exactly once per city (examples: city hall, fire station, police station, hospital, school) and possibly dozens of buildings of “extra” type, such as dance clubs.

(2) In certain views, all buildings (regardless of city, etc.) are to be filtered according to different criteria, e.g., other_criterion.

Problem/concern:

In a city_detail view, I would have to loop over any buildings of “extra” type, which is OK and normal.

But I am not sure how to efficiently retrieve the city’s “hospital” building, which is of “basic” type, so I must do this for every city anyway because exactly one such hospital exists in each city (this is ensured at city creation time).

There will be at most a dozen of “basic” building types, of which about half will be presented all the time.

I’m inclined towards writing convenience methods on the City model, and I face three options:

(A1) Via try and index: .filter(...)[0]

(A2) Via try and .get(...)

(A3) Via try and .filter(...).latest()

But none of those seem elegant.
Or is one of these three options good to combine with some sort of caching, like in Django’s get_profile() method on the User model? Unfortunately, I have no experience with caching yet.

Is it nuts to use the following option?

(B) specific FKs in the City model, one for each of the most important basic types

Question:

Which option makes sense the most?
Or is the schema generally faulty for this kind of scenario?

Especially regarding DB performance, what do you suggest? Do I need a completely different approach?

Please advise! 🙂

Thanks in advance!

  • 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-13T00:58:48+00:00Added an answer on May 13, 2026 at 12:58 am

    If a city can have no more than one each of city hall, fire station, police station, hospital, school etc. then I think the most straightforward way to enforce this is to declare each one as a field on the model:

    class City(models.Model):
        name = models.CharField(...)
        city_hall = models.ForeignKey(Building)
        fire_station = models.ForeignKey(Building)
        # ... et cetera
    

    If you find this too “messy” in your City model, you might consider having an intermediate CityBuildings model:

    class CityBuildings(models.Model):
        city_hall = models.ForeignKey(Building)
        fire_station = models.ForeignKey(Building)
        # ... et cetera
    
    class City(models.Model):
        name = models.CharField(...)
        buildings = models.OneToOneField(CityBuildings)
    

    Then you refer to buildings as, for example, city.buildings.fire_station

    These are just suggestions… I am not sure if either way is more “correct”

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

Sidebar

Related Questions

No related questions found

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.