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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T08:04:09+00:00 2026-06-04T08:04:09+00:00

I asked this question before, but I received no solutions, so I have tried

  • 0

I asked this question before, but I received no solutions, so I have tried to make the question concise & clearer this time.

My database has the following simplified schema:

class RIAchievement(models.Model):
  riAchievementID = models.AutoField(primary_key=True, db_column="riAchievementID")
  userLanguageVersionID = models.ForeignKey(UserLanguageVersion, db_column="userLanguageVersionID", related_name="riAchievement_userLanguageVersionID")
  class Meta:
    db_table="riAchievement"

class UserLanguageVersion(models.Model):
  userLanguageVersionID = models.AutoField(primary_key=True, db_column="userLanguageVersionID")
  languageCodeID = models.ForeignKey(LanguageCode, db_column="languageCodeID", related_name="userLanguageVersion_languageCodeID")
  class Meta:
    db_table="userLanguageVersion"

class LanguageCode(models.Model):
  languagecodeID = models.AutoField(primary_key=True, db_column="languageCodeID")
  class Meta:
    db_table="languageCode"

class Flag(models.Model):
  flagID = models.AutoField(primary_key=True, db_column="flagID")
  languageCodeID = models.ForeignKey(LanguageCode, db_column="languageCodeID", related_name="flag_languageCodeID")
  flagIconPath = models.CharField(max_length=255, db_column="flagIconPath")
  class Meta:
    db_table="flag"

Essentially, riachievement can have many userlanguageversion s and userlanguageversion can have many languagecode s, and flag can have many languagecode s.

Using select_related does not return flag.flagIconPath because of the 1 to many relationship, so the Django docs state I must use prefetch_related, with the related name of the 1 to many foreign key.

So I amend my code in my view.py:

from django.shortcuts import render
from app_data.models import RIAchievement

def ri_achievements(request):

  qs = RIAchievement.objects.select_related("riachievement", "userlanguageversion", "languagecode", "flag_languageCodeID").all()

  return render(request, 'index.html',{'qs': qs})

And my index.html:

{% for ri_achievement in qs %}
  {{ ri_achievement.userLanguageVersionID.langaugeCodeID.flag_languageCodeID.flagIconPath }}
{% endfor %}

But, this code returns nothing.

Can anyone offer some advice, as I cannot see what I have done wrong?

  • 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-04T08:04:10+00:00Added an answer on June 4, 2026 at 8:04 am

    I suggest you use the django shell python manage.py shell to figure out your query – it’s going to be very difficult to “figure it out” via the template as it supresses errors.

    For example, it looks like you have a spelling error, but the template wouldn’t complain about it.

    Let’s break down your attempted query into python so that we can use comments

    flagIconPath = (ri_achievement
        .userLanguageVersionID # correct. direct FK 
        .langaugeCodeID # spelling error. but otherwise ok. direct FK
        # this is now a one to many relationship - you now have a related manager
        # NOT a single object. Many flags to one Code
        .flag_languageCodeID
        .latest('id') # you must pick which one you want - for example, the latest ID
        .flagIconPath)
    

    As for making this query more efficient, your select related call would need to include the full paths through these models. You are passing attributes as if they are all foreign keys on the original model.

    qs = RIAchievement.objects.select_related('userLanguageVersionID__languageCodeID')
    

    I’m unsure of the prefetch call at this point but it should probably look something like

    qs.prefetch_related('userLanguageVersionID__languageCodeID__flag_languageCodeID')
    

    With all of these nested calls, a prefetch that far could.. make a gigantic query. Are you sure this is what you want?

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

Sidebar

Related Questions

I have asked this question before, but have not received any real answer. How
i have asked this question before but the answer received was not applicable in
I have asked this before but I didn't get the question right so the
This question may have been asked before, but I had trouble finding an answer,
I asked this question before but with less information than I have now. What
I asked this question before but didn't make it clear that I meant in
I have asked this question before but I haven't got an answer, so I'm
I have been asked this questions before but this time i want to know
I have asked a question on this before, but again ran into problems and
i have asked this question before but no answer was there. so asking again.

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.