Note that there are some explanatory texts on larger screens.

plurals
  1. PODB / performance: layout of django model that rarely refers to its parent more than once
    primarykey
    data
    text
    <p>I have an app that's about presenting fictional simplified cities.</p> <p>Please consider the following Django models:</p> <pre><code>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' </code></pre> <p>Explanations for choosing this setup:</p> <p>(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.</p> <p>(2) In certain views, all buildings (regardless of city, etc.) are to be filtered according to different criteria, e.g., <code>other_criterion</code>.</p> <p><b>Problem/concern:</b></p> <p>In a <code>city_detail</code> view, I would have to loop over any buildings of "extra" type, which is OK and normal.</p> <p>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).</p> <p>There will be at most a dozen of "basic" building types, of which about half will be presented all the time. </p> <p>I'm inclined towards writing convenience methods on the City model, and I face three options:</p> <p>(A1) Via <code>try</code> and index: <code>.filter(...)[0]</code></p> <p>(A2) Via <code>try</code> and <code>.get(...)</code></p> <p>(A3) Via <code>try</code> and <code>.filter(...).latest()</code></p> <p>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 <code>get_profile()</code> method on the <code>User</code> model? Unfortunately, I have no experience with caching yet.</p> <p>Is it nuts to use the following option?</p> <p>(B) specific FKs in the City model, one for each of the most important basic types</p> <p><b>Question:</b></p> <p>Which option makes sense the most?<br> Or is the schema generally faulty for this kind of scenario?</p> <p>Especially regarding DB performance, what do you suggest? Do I need a completely different approach?</p> <p>Please advise! :)</p> <p>Thanks in advance!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload