title: How to keep track of how many foreign keys are connected to an object?
body: <p>I have two tables in a database (that I've created): <code>Entry</code> and <code>Nation</code>. Every time a new entry is created, the user has to fill in what Nation it relates to. So an entry has two fields: <code>Content</code> [a charfield] and <code>Nation</code> [a foreign key to a Nation]. The thing is, on the homepage of the site, the most popular nations are displayed in order of how many entries they have. It seems inefficient to constantly look up how many foreign keys in the <code>Entry</code> table relate to each <code>Nation</code>, so I'm wondering how I can keep a counter. By this I mean, if I create an extra integer field for the Nations, how could I make it so that every time an entry is created, it increments the correct nation by 1, and every time an entry is destroyed, it decreases the correct nation by 1. </p>