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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T03:40:04+00:00 2026-06-13T03:40:04+00:00

Using django, I’m trying to get a QuerySet of FK relationships from another QuerySet.

  • 0

Using django, I’m trying to get a QuerySet of FK relationships from another QuerySet.

That is, given a relation like so:
ParentTable (ParentID) and
ChildTable(ChildID, ParentID

I have a particular QuerySet
children = Child.objects.filter(**some_filters)
and I need a QuerySet for the Parent objects “referenced” in the children QuerySet.

My initial thought was to use only, like so:
Parent.objects.filter(id__in=child.only('parent')), but inspecting the SQL query shows that gives a WHERE clause along the lines of ParentID IN (SELECT subq.ChildID FROM ... which obviously yields undesired results. (inspection of the only method shows my use here is incorrect, so this behaviour is expected)

The desired query would be something ParentID IN (SELECT subq.ParentID FROM ...

  • 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-13T03:40:05+00:00Added an answer on June 13, 2026 at 3:40 am

    Well you have answered your question:

    Consider this, when create a Foreign Key reference then, Django automatically creates a reverse reference for you which is why you are able to filter results based on the child. Moreover, if you want to abide this behavior you can use Symmetrical=False kwargs in the Model Field to not generate the reverse relationship.

    class Parent(models.Model):
    ...
    
    class Child(models.Model):
         parent = models.ForeignKey(Parent)
    

    You can always make the query:

    Parent.objects.filter(child__in = Childrens.objects.all())
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using Django 1.1, I am trying to select the maximum value from a varchar
using django 1.4, I am trying to Post and it seems like its not
I'm using Django 1.3, and would like to get an array of objects using
Am using django social-auth authentication for my app.After login to facebook or twitter from
I using Django tagging project. That is was very stable project. Working on django
I am using django-registration and would like to have a different page for failed
Using django-social-auth to grab user data from facebook, it is returning a list of
Using django 1.4 and have seen that when you use startproject it now creates
(Using django as an example; the ForeignKey is implicit in the example) >>> from
I'm using Django-social-auth for the first time, and it seems that all the new

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.