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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T22:22:45+00:00 2026-06-04T22:22:45+00:00

E.g. models: class Player(models.Model): Team = models.ForeignKey(Team) From an efficiency point of view, if

  • 0

E.g. models:

class Player(models.Model):
    Team = models.ForeignKey(Team)

From an efficiency point of view, if you access the “team” of a player multiple times, is it more efficient to store the team in a temp variable?

Is this less efficient:

player = Player.objects.get(id=1)
print "Player is a member of " + str(player.team)
player.team.printRoster()

than this?:

player = Player.objects.get(id=1)
temp = player.team
print "Player is a member of " + str(temp.team)
temp.printRoster()

I feel the answer is No, they are both the same. But have heard different feedback from colleagues and wanted to get an objective viewpoint.

  • 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-04T22:22:48+00:00Added an answer on June 4, 2026 at 10:22 pm

    It’s specified here.

    Django caches it for you. The question is what are you going to do with these “foreign key” references. For example, if you have a foreign key to a model which its unicode method access another foreign key to print a “friendly” unicode string, depending on how you make the queryset you may hit the database another time.

    Let me give you an example to clarify:

    class Player(models.Model):
        pname = models.CharField(max_length=20)
        team = models.ForeignKey('Team')
    
    class Team(models.Model):
        tname = models.CharField(max_length=20)
        country = models.ForeignKey('Country')
    
        def __unicode__(self):
            return self.tname + u' is from ' + self.country.cname
    
    class Country(models.Model):
        cname = models.CharField(max_length=10)
    

    When you do this:

    player = Player.objects.get(name=u'Messi')
    print player.team
    

    you will hit the database three times, one fot getting the player, one of getting the team and another time to retrieve the country name. If i’m not wrong, the next time you make that “print” statement, django won’t hit the database again (i may be wrong, but i think it works that way).

    Imagine now that you want to do this for every player, and you have a lot of them. You are going to hit the database three times per player and that can’t be good. If you change your query a little, you can do the same hitting the database just once. This is how you can do it:

    players = Player.objects.all().select_related('team__country')
    for player in players:
        print player.team
    

    I hope this help you to understand how querysets work on Django. Try to read the documentation i linked here. One excelent tool that can help you measuring your querys efficiency is django_toolbar. I highly recommend it.

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

Sidebar

Related Questions

I have the following models defined: class Player(models.Model): Team = models.ForeignKey(Team) Name = models.CharField(max_length=200)
I have the following models: class Fixture(models.Model): id = models.AutoField(primary_key=True) home_team = models.ForeignKey(Player, related_name=home)
I have the following models: class LibraryEntry(models.Model): player = models.ForeignKey(Player) player_lib_song_id = models.IntegerField() title
I've got these models class PlayersToTeam < ActiveRecord::Base belongs_to :player belongs_to :team accepts_nested_attributes_for :player
i have the following models setup class Player(models.Model): #slug = models.slugField(max_length=200) Player_Name = models.CharField(max_length=100)
My model: class Player(models.Model): player_name = models.CharField(max_length=50) player_email = models.CharField(max_length=50) def __unicode__(self): return self.player_name
Consider these pseudo models: class City(models.Model): name = models.CharField() region = models.ForeignKey(Region) class Region(models.Model):
I currently have the following models: class Player < ActiveRecord::Base belongs_to :team belongs_to :user
I have a django model like this: class Player(models.Model): name = models.CharField() batting =
I have generated a strongly-type view from a model class that I have created

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.