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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T01:42:43+00:00 2026-06-15T01:42:43+00:00

I have two tables in a database (that I’ve created): Entry and Nation .

  • 0

I have two tables in a database (that I’ve created): Entry and Nation. Every time a new entry is created, the user has to fill in what Nation it relates to. So an entry has two fields: Content [a charfield] and Nation [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 Entry table relate to each Nation, 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.

  • 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-15T01:42:44+00:00Added an answer on June 15, 2026 at 1:42 am

    You could use the post_save signal, which is sent at the end of the save() method.

    First, add (as you said) a counter = IntegerField(default=0) to your Nation model.

    Then, everytime a new Entry is created, increase that counter.

    from django.db.models.signals import post_save
    
    # Your models ...
    
    def increase_nation_counter(instance, created, **kwargs):
        if created: # Just increase counter if it's a new Entry
            instance.nation.counter += 1
            instance.nation.save()
    
    post_save.connect(increase_nation_counter, sender=Entry)
    

    Then, you could sort your Nations with a simple query:

    Nation.objects.order_by('-counter')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an Access database that has two tables that are related by PK/FK.
I have one database that has two tables. One table (Table A) has 3
I have a database that has two tables locations and classes. I am familiar
I have two tables in my database that look like that: Customer: C_ID city
I have two tables in my database schema that represent an entity having a
I have a database that which contains two tables, links and tour. Tour is
I have two separate sets of tables in the same database that model the
I have a database two tables and a linking table that I need a
I have a very simple database table that joins two other tables in a
Basically I have a database with two tables, that is, Updates table and Images

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.