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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:38:10+00:00 2026-05-26T02:38:10+00:00

I have a few data models (tables) which are associated with each other. If

  • 0

I have a few data models (tables) which are associated with each other. If a model has a related field pointing to another model, no problem, i can access that easy in the templates but how do I make calls that have reverse-only relation?

Example:

Company may have any number of Location and each Location may have any number of Contact.

So the Company table mentions neither the Location or Contact, but Location has a fk to Company. and Contact has a fk to Location.

In the template, I’d like to display Companies that match certain criteria. With them, I’d like their locations and their locations contacts to also display.

My thought is, maybe I make the Company query first, then make another for Location (and Contact) and some how inject them into the same dict but that sounds awfully confusing to me and this, im sure, is a common pattern.

Any tips?

  • 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-05-26T02:38:10+00:00Added an answer on May 26, 2026 at 2:38 am

    When you make a ForeignKey field on one model, the target model automatically gets a attribute called <referring_model>_set – so in your example, each Company instance has a location_set attribute, and each Location has a contact_set attribute. These attributes actually refer to Manager instances, similar to Company.objects, that return only objects related to the current instance.

    You can use these backwards references to iterate through the related objects in your templates, e.g.:

    <ul>
    {% for location in company.location_set %}
        <li>
            <em>{{ location.name }}</em>
            <ul>
            {% for contact in location.contact_set %}
                <li>{{ contact.name }}</li>
            {% endfor %}
            </ul>
        </li>
    {% endfor %}
    </ul>
    

    You can read more about this kind of backwards relationship in the Django docs.

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

Sidebar

Related Questions

in my application I have a few CListCtrl tables. I fill/refresh them with data
I have a DataGrid component that displays a few columns of data. It has
I have a data access library that has a few classes that all implement
I have a few models that contain static data, and I'm trying to cache
I had a home page which have a few tables that refresh itself every
I have models corresponding to database tables. For example, the House class has color,
We have a data model that contains large amount of columns in few key
I have a few questions about data synchronization. The architecture does not seem to
I'm manipulating few data with PHP, I have the choice between Json and XML.
I have a few GridItem components that gets filled with dynamic data. Sometimes this

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.