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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T16:07:25+00:00 2026-05-22T16:07:25+00:00

Situation I have 3 models. Model A, model B and model C. class A(Model):

  • 0

Situation

I have 3 models. Model A, model B and model C.

class A(Model):
    B_id = ForeignKey(B)
    id = IntegerField()
    pub_date = DateField()

class B(Model):
    id = IntegerField(primary_key = True)

class C(Model):
    B_id = ForeignKey(B)
    pub_date = DateField()
    information = TextField()

Model A and model C both have a field called B_id, which is a foreign key to model B. So there is no direct connection from B to A or C.

Goal

I’m given an ID from model A. Now I need all informations from C where the pub_date matches the pub_date in A and the B_id matches the B_id in A.

In SQL, this would be a pretty easy query:

SELECT C.information FROM A JOIN C USING(B_id, pub_date) WHERE A.id = 1234;

But I simply can’t figure out how to do this in Django ORM syntax, as a filter()-clause only works if there is a direct connection path from C to A.

—

Edit: Please note that there is more than one row in A that matches one id.

  • 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-22T16:07:26+00:00Added an answer on May 22, 2026 at 4:07 pm

    EDIT:

    Try this:

     A.objects.filter(B_id__c__pub_date=F('pub_date'))
    

    I’m not 100% sure about the capitalization on the filter string, but the F() predicate is what you are looking for. See the docs.

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

Sidebar

Related Questions

I have the following situation: class Professor(models.Model): default_address = models.OneToOneField('Address') class Address(models.Model): name =
I have the following multi-table inheritance situation: from django.db import Models class Partner(models.Model): #
I have these models in my app models.py: class A(models.Model): #some details pass class
I have a blog-like application with stories and categories: class Category(models.Model): ... class Story(models.Model):
I have models (simplified example): class Group(models.Model): name = models.CharField(max_length = 32) class Person(models.Model):
I have the following model : class A: b = ForeignKey('B') class B: a
I have a model that has a unique generic foreign key relationship: class Contact(models.Model):
In this situation I have two models, Comment and Score. The relationship is defined
I have a situation where I have two models, companies and permissions, where companies
I have an unusual situation to model in a MS SQL Server database: the

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.