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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:11:18+00:00 2026-05-28T03:11:18+00:00

I recently started evaluating Django for migrating our archaic web application written 10 years

  • 0

I recently started evaluating Django for migrating our archaic web application written 10 years ago. I have been reading up Django documentation for the last few days, but haven’t been able to figure out the best way to achieve a multi table database join in my case:

Model:

class Product(models.Model):
    productid = models.IntegerField(primary_key=True, db_column='ProductId') 
    productname = models.CharField(max_length=120, db_column='ProductName')

class Testcases(models.Model):
    testcaseid = models.IntegerField(primary_key=True, db_column='TestCaseId')
    testcasename = models.CharField(max_length=240, db_column='TestCaseName')

class Testmatrix(models.Model):
    testmatrixid = models.IntegerField(primary_key=True, db_column='TestMatrixId')
    productid = models.ForeignKey(Product, db_column='ProductId')
    testcaseid = models.ForeignKey(Testcases, db_column='TestCaseId')

class Status(models.Model):
    testmatrixid = models.ForeignKey(Testmatrix, db_column='TestMatrixId')
    title = models.CharField(max_length=240, db_column='Title', blank=True)

(Note that model was generated by inspectdb and I’d prefer not to modify it at this point in time)

View:

from django.shortcuts import render_to_response
from mysite.testmatrix.models import Product, Testcases, Testmatrix, Status

def get_products(request):
    tm = list(Testmatrix.objects.filter(productid='abc'))
    return render_to_response('products.html', {'tm': tm})

template is designed to be minimal at this point to help focus on the real issue in (views/model).

Template: (products.html)
{% extends "main.html" %}

{% block body %}
<table>
{% for tm in tm %}
    <tr>
            <td>{{ tm.testmatrixid }}</td>
    <td>{{ tm.testcaseid.testcasename }}</td>
    </tr>
{% endfor %}
</table>
{% endblock %}

Problem:

Although I’m able to join Testmatrix and Testcase models, I am unable to generate an equivalent queryset by joining all of TestMatrix, TestCase, Status records on say productid=’abc’

I tried the following:

1) Use select_related between Testmatrix and Testcases and Product tables and was able to access attributes across all three models (testmatrixid, productid, productname, testcaseid, testcasename). However I’m not sure how to extend this auto foreign key referencing to Status model. This would have been easier if all Foreign Keys were defined within Testmatrix itself. But Status has a Foreign Key to TestMatrix.

2) I tried using something like: entries = Status.objects.filter(testmatrixid__productid=pid). This again gave me a queryset as a result of joining Testmatrix and Status, but not Testcases.

Pardon any blaring mistakes or bloopers. This is my very first post!

  • 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-28T03:11:19+00:00Added an answer on May 28, 2026 at 3:11 am

    So you need to access a related_object. It is very simple.

    First, add related_name here:

    class Status(models.Model):
        testmatrixid = models.ForeignKey(Testmatrix, db_column='TestMatrixId', related_name='statuses')
    

    Now you can get all the statuses for desired Testmatrix like

    test_matrix.statuses.all()
    

    If you don’t want to hit DB, when you access statuses, don’t forget to use select_related.

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

Sidebar

Related Questions

I recently started using Silex. I have been grouping certain functionality of my application
I've recently started coding in Lisp, and have already been most impressed with macros
I recently started using Eclipse at work for my Java servlet projects. I've been
We recently started using maven for dependency management. Our team uses eclipse as it's
I´ve recently started looking a web service calls using PHP. I can´t get the
I recently started using swing after years of using C# and I was wondering:
I recently started compiling my iPhone application against the 3.0 OS. The app worked
I recently started reading Paul Grahams 'On Lisp', and learning learning clojure along with
I recently started reading up on the out-of-browser ability of Silverlight 3. I also
Recently our company has started measuring the cyclomatic complexity (CC) of the functions in

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.